CALayer上动画的暂停和恢复

iOS10自带了暂停动画的方法

在iOS10以下 需要自己实现

1.暂停layer动画

- (void)pauseLayer:(CALayer*)layer 
{ 
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
    layer.timeOffset = pausedTime; 
    //暂停
    layer.speed = 0.0; 

} 

2.恢复layer上的动画

- (void)resumeLayer:(CALayer*)layer 
{ 
    CFTimeInterval pausedTime = [layer timeOffset]; 
    //暂停时间
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; 
    取消暂停
    layer.timeOffset = 0.0; 
    layer.beginTime = 0.0; 
    layer.beginTime = timeSincePause;
    //恢复动画 
    layer.speed = 1.0; 

}

你可能感兴趣的:(CALayer上动画的暂停和恢复)