views:

35

answers:

1

I am running into a problem where a restart of my iPhone app causes animations to stop. More specifically, I have the following animation set and running:

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"];

When I press the home key (iOS 4 so it is still 'running' in the background) and then relaunch the program, the animation has stopped. Is there any way to prevent this or easily restart them?

A: 

There are two methods in your app delegate where you can pass down information to your view controller that is performing the animation.

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

Of course this means you'll need a reference to your view controller that is performing the animation in your app delegate, or you could use notifications to pass the notification along. Anyhow, the bottom line is you'll have to watch for the app becoming active again and restart the animation.

Matt Long
Thanks. This worked but in the end we just set UIApplicationExitsOnSuspend to true since the user would need to resync with the server upon resuming anyway.
Chase
Yeah. I'm finding more and more that exiting is often a better solution.
Matt Long