54

我有一个基本的 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:rotationAnimation forKey:@"rotationAnimation"];
4

6 回答 6

165

最近出现的 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,它允许以交互方式处理动画,例如,它可以轻松暂停和恢复动画或“寻找”某个特定的进度值。

于 2010-06-09T07:46:38.463 回答
18

Swift 3 的答案:

学分@弗拉基米尔

代码:

 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
}
于 2016-11-03T11:20:30.383 回答
7

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

CALayer *pLayer = [myView.layer presentationLayer];
myView.layer.transform = pLayer.transform;
[myView.layer removeAnimationForKey:@"rotationAnimation"];
于 2010-02-23T01:55:38.533 回答
3

感谢@Vladimir 和@Supratik-Majumdar

Swift 5.1 作为 CALayer 扩展

    extension CALayer
    {
        func pauseAnimation() {
            if isPaused() == false {
                let pausedTime = convertTime(CACurrentMediaTime(), from: nil)
                speed = 0.0
                timeOffset = pausedTime
            }
        }

        func resumeAnimation() {
            if isPaused() {
                let pausedTime = timeOffset
                speed = 1.0
                timeOffset = 0.0
                beginTime = 0.0
                let timeSincePause = convertTime(CACurrentMediaTime(), from: nil) - pausedTime
                beginTime = timeSincePause
            }
        }

        func isPaused() -> Bool {
            return speed == 0
        }
    }
于 2019-11-27T23:55:32.987 回答
0

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

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

希望它可以帮助你。

于 2011-12-12T08:02:48.567 回答
-1

最简单的一个

self.viewBall.layer.position = self.viewBall.layer.presentationLayer().position
于 2015-05-29T11:46:14.450 回答