美文网首页
CoreAnimation的transform和frame

CoreAnimation的transform和frame

作者: iDeveloper | 来源:发表于2017-07-09 16:53 被阅读209次

    案例:

    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

          本文链接:https://www.haomeiwen.com/subject/rdzshxtx.html