有没有办法暂停CABasicAnimation?

我有一个iPhone的基本旋转animation。 有什么办法可以“暂停”animation,以保持视图的位置? 我想这样做的一个方法是导致animation“完成”,而不是调用“删除”,我怎么办呢?

CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2]; rotationAnimation.duration = 100; rotationAnimation.cumulative = YES; rotationAnimation.repeatCount = HUGE_VALF; rotationAnimation.removedOnCompletion = NO; rotationAnimation.fillMode = kCAFillModeForwards; [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

最近出现的苹果技术说明QA1673介绍了如何暂停/恢复图层的animation。

暂停和恢复animation列表如下:

 -(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; } 

编辑: iOS 10引入了新的API – UIViewPropertyAnimator,允许更交互地处理animation,例如,它可以很容易地暂停和恢复animation或“寻找”到一些特定的进度值。

设置视图图层的当前状态以匹配presentationLayer的状态,然后删除animation:

 CALayer *pLayer = [myView.layer presentationLayer]; myView.layer.transform = pLayer.transform; [myView.layer removeAnimationForKey:@"rotationAnimation"]; 

回答Swift 3:

致谢@Vladimir

码:

  func pauseAnimation(){ var pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) layer.speed = 0.0 layer.timeOffset = pausedTime } func resumeAnimation(){ var pausedTime = layer.timeOffset layer.speed = 1.0 layer.timeOffset = 0.0 layer.beginTime = 0.0 let timeSincePause = layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pausedTime layer.beginTime = timeSincePause } 

您可以使用计时器或处理animation委托方法:

 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag 

这是我的代码:

 // ... [self startAnimation]; // ... - (void)startAnimation { CABasicAnimation* rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; rotationAnimation.fromValue = [NSNumber numberWithFloat:0]; rotationAnimation.toValue = [NSNumber numberWithFloat: M_2_PI]; rotationAnimation.duration = 1.0; rotationAnimation.cumulative = YES; // rotationAnimation.repeatCount = 0; // <- if repeatCount set to infinite, we'll not receive the animationDidStop notification when the animation is repeating rotationAnimation.removedOnCompletion = NO; rotationAnimation.fillMode = kCAFillModeForwards; rotationAnimation.delegate = self; // <- hanlde the animationDidStop method [myView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { if (shouldContinueAnimation) // <- set a flag to start/stop the animation [self startAnimation]; } 

希望它可以帮助你。

最简单的一个

 self.viewBall.layer.position = self.viewBall.layer.presentationLayer().position