CoreAnimation的transform和frame

案例:

loadingImageView, 需要不断做旋转动画,在某个时间点需要进行移动动画.

旋转动画如下:

- (void)rotateLoading{
    [self.loadingImageView.layer removeAllAnimations];
    CABasicAnimation *rotateAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotateAnim.fromValue = [NSNumber numberWithFloat:0];
    rotateAnim.toValue = [NSNumber numberWithFloat:2*M_PI];
    rotateAnim.duration = 0.6;
    rotateAnim.repeatCount = MAXFLOAT;
    [self.loadingImageView.layer addAnimation:rotateAnim forKey:@"rotate"];
}

移动动画如下:

[UIView animateWithDuration:0.5 animations:^{
    self.loadingImageView.frame = self.originalFrame;
 } completion:^(BOOL finished) {
    if (finished) {
        [self.loadingImageView.layer removeAllAnimations];
        self.isLoading = NO;
    }
 }];

动画不正常.

分析:

原来在Frame属性中有如下说明:

@interface UIView(UIViewGeometry)
// animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.
@property(nonatomic) CGRect            frame;

即修改transform后, frame就不能正确地表示它的实际位置. 应该使用bounds + center 来代替.

解决:

改成对center做动画:

[UIView animateWithDuration:0.5 animations:^{
    self.loadingImageView.center = self.originalCenter;
} completion:^(BOOL finished) {
    if (finished) {
        [self.loadingImageView.layer removeAllAnimations];
        self.isLoading = NO;
     }
 }];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容