有没有办法暂停CABasicAnimation?

我有一个基本的iPhone旋转动画。有什么方法可以“暂停”动画,以便维持视图的位置?我想这样做的一种方法是使动画“完成”,而不是对其调用“删除”,我该怎么做?


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:rotationAn


繁星coding
浏览 2011回答 3
3回答

偶然的你

最近出现的Apple技术说明QA1673描述了如何暂停/恢复图层的动画。暂停和恢复动画列表如下:-(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,该API可以更交互地处理动画,例如,它使暂停和恢复动画或“搜索”到特定进度值变得容易。

HUH函数

回答Swift 3:积分@Vladimir码: func pauseAnimation(){  let pausedTime = layer.convertTime(CACurrentMediaTime(), fromLayer: nil)  layer.speed = 0.0  layer.timeOffset = pausedTime}func resumeAnimation(){  let 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}

心有法竹

您可以使用计时器或处理动画委托方法:- (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 repeatingrotationAnimation.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&nbsp; &nbsp; [self startAnimation];}希望它能对您有所帮助。
打开App,查看更多内容
随时随地看视频慕课网APP