【问题标题】:pause and resume animation from last position从最后一个位置暂停和恢复动画
【发布时间】:2014-03-26 11:49:22
【问题描述】:

我有一个带有背景图像的按钮,当应用程序运行时,我希望按钮旋转,当应用程序进入后台时,按钮应该停止旋转。

我调用了无限旋转按钮的方法startSpiningAnimation
当应用程序进入后台时,我调用stopSpinningAnimation 来停止旋转动画。

- (void)startSpiningAnimation {
    [self stopSpinningAnimation];

    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.fromValue = [NSNumber numberWithFloat:0.0];
    animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
    animation.duration = 10.0f;
    animation.repeatCount = INFINITY;
    [self.button.layer addAnimation:animation forKey:@"SpinAnimation"];
}

- (void)stopSpinningAnimation {
    [self.button.layer removeAllAnimations];
}    

- (void)handleApplicationDidBecomeActiveNotification:(NSNotification *)not {
    [self startSpiningAnimation];
}

- (void)handleApplicationWillResignActiveNotification:(NSNotification *)not {
    [self stopSpinningAnimation];
}

一切都很好,我唯一的问题是每次应用程序进入前台并且动画开始时,它都是从原始位置开始的。

(我完全理解它为什么会发生,当应用程序进入后台时,我正在删除所有动画。我只是不知道如何解决这个问题)

我想要的是按钮旋转动画将从上一个角度继续。

【问题讨论】:

标签: ios iphone objective-c core-animation cabasicanimation


【解决方案1】:

您可以在 update() 方法中收集所有“动态”行为,该方法仅在应用程序处于前台时调用。

因此,您将拥有创建、更新和停止动画的方法。当您的应用程序失去焦点时,不再调用更新并且不再更新动画。但是它仍然存在,并且相关的变量保留在它们以前的状态。

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 2012-04-17
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2011-07-31
    相关资源
    最近更新 更多