美文网首页
iOS缩放动画CABasicAnimation对比animate

iOS缩放动画CABasicAnimation对比animate

作者: 倪大头 | 来源:发表于2019-01-07 11:18 被阅读29次

    一开始我是这样写的

    - (void)voiceCircleRun {
         __weak typeof(self) weakSelf = self;
         [UIView animateWithDuration:1 animations:^{
            weakSelf.transform = CGAffineTransformMakeScale(1.4, 1.4);
         } completion:^(BOOL finished) {
            [UIView animateWithDuration:1 animations:^{
                weakSelf.transform = CGAffineTransformMakeScale(1.0, 1.0);
            } completion:^(BOOL finished) {
                [weakSelf voiceCircleRun];
            }];
        }];
    }
    

    表面看上去没什么问题,但当这个动画所在的view不在当前视图,或者应用切到后台,cpu会飙升到100多,下面用CABasicAnimation代替

    - (void)voiceCircleRun {
        CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.duration = 1;
        scaleAnimation.repeatCount = HUGE_VALF;
        scaleAnimation.autoreverses = YES;
        //removedOnCompletion为NO保证app切换到后台动画再切回来时动画依然执行
        scaleAnimation.removedOnCompletion = NO;
        scaleAnimation.fromValue = @(1.0);
        scaleAnimation.toValue = @(1.4);
        [self.layer addAnimation:scaleAnimation forKey:@"scale-layer"];
    }
    
    //不使用时记得移除动画
    - (void)voiceCircleStop {
        [self.layer removeAllAnimations];
    }
    

    相关文章

      网友评论

          本文标题:iOS缩放动画CABasicAnimation对比animate

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