一开始我是这样写的
- (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];
}
网友评论