-(void)pauseLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; layer.speed = 0.0; layer.timeOffset = pausedTime; } -(void)resumeLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer timeOffset]; layer.speed = 1.0; layer.timeOffset = 0.0; layer.beginTime = 0.0; CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; layer.beginTime = timeSincePause; }
如果在一個view中我們添加了多個動畫,(不是組合動畫),同樣容易實現動畫的暫停、恢復,方法就是為每個動畫層layer調用上面的方法,比如我的方法如下,我用的是三個單獨動畫的組合效果,就是如下調用的
-(void)stopAnimation; { NSLog(@"停止動畫"); //[_customView.layer removeAllAnimations]; [self pauseLayer:_customView1.layer]; [self pauseLayer:_customView2.layer]; [self pauseLayer:_customView3.layer]; } -(void)resumeAnimation { [self resumeLayer:_customView1.layer]; [self resumeLayer:_customView2.layer]; [self resumeLayer:_customView3.layer]; }