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;
     }
 }];

你可能感兴趣的:(CoreAnimation的transform和frame)