从上一个位置暂停和恢复动画
问题描述:
我有一个带背景图像的按钮,当应用程序运行时我希望按钮旋转,当应用程序转到背景时,按钮应该停止旋转。从上一个位置暂停和恢复动画
我打电话给无限旋转按钮的方法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];
}
一切都很正常我唯一的问题是,每一个应用程序来前景和动画启动时,它从原来的位置开始。
(我完全理解为什么会发生,当应用程序被切换到后台obesely我删除所有动画..我只是不知道如何解决这个问题)
我想是该按钮旋转动画将继续从最后一个角度来看。
答
您可以收集update()
方法中的所有'动态'行为,该方法仅在应用程序处于前景时才会调用。
因此,您将有创建,更新和停止动画的方法。当您的应用程序离焦时,不再调用更新,并且动画不再更新。但它仍然存在,并且相关变量将保留在先前的状态中。
[有没有办法暂停CABasicAnimation?](http://stackoverflow.com/questions/2306870/is-there-a-way-to-pause-a-cabasicanimation) –