重新启动应用程序时,CABasicAnimation停止

时间:2010-10-04 11:29:19

标签: iphone animation core-animation

我遇到了一个问题,重启我的iPhone应用程序导致动画停止。更具体地说,我有以下动画设置和运行:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = 1.0;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.repeatCount = 1e100f; // Infinite
animation.autoreverses = YES;
animation.fromValue = animationStartPath;
animation.toValue = animationFinishPath;
[view.layer addAnimation:animation forKey:@"animatePath"];

当我按下主页键(iOS 4使其仍在后台“运行”)然后重新启动程序时,动画已停止。有什么方法可以防止这种情况或轻松重启吗?

1 个答案:

答案 0 :(得分:1)

您的应用委托中有两种方法,您可以将信息传递给正在执行动画的视图控制器。

- (void)applicationWillResignActive:(UIApplication *)application
{
  // Make note of whether or not the animation is running.
  // Using NSUserDefaults is probably the simplest
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  // Check your user default setting to see if the animation
  // was running when the app resigned active and then restart it
}

当然,这意味着您需要对应用程序委托中执行动画的视图控制器的引用,或者您可以使用通知来传递通知。无论如何,最重要的是你必须注意应用程序再次变为活动状态并重新启动动画。