iOS缩放动画CABasicAnimation对比animateWithDuration

一开始我是这样写的

- (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];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容