Created
August 31, 2016 02:53
-
-
Save ilklatte/b7d7570da30bce97d6c9121066c5613d to your computer and use it in GitHub Desktop.
iOS CALayer 的 pause 和 resume
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 使用CABasicAnimation时,如果需要暂停动画、恢复动画时可以通过pauseLayer | |
- (void)pauseLayer:(CALayer*)layer { | |
if (layer.speed == 0.0) { | |
return; | |
} | |
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; | |
layer.speed = 0.0; | |
layer.timeOffset = pausedTime; | |
} | |
- (void)resumeLayer:(CALayer*)layer{ | |
if (layer.speed > 0) { | |
return; | |
} | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment