Skip to content

Instantly share code, notes, and snippets.

@cobysy
Created June 10, 2014 07:04
Show Gist options
  • Save cobysy/c8d37924a15d6b9d4007 to your computer and use it in GitHub Desktop.
Save cobysy/c8d37924a15d6b9d4007 to your computer and use it in GitHub Desktop.
Resume animation rotation on leaving the background state to becoming the active app.
- (void)registerForAppStateNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)applicationDidEnterBackground
{
self.suspendedRotation = [self.imageView.layer.presentationLayer valueForKeyPath:@"transform.rotation.z"];
}
- (void)applicationWillEnterForeground
{
// restart animation from where we left off
CABasicAnimation* rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotateAnimation.fromValue = self.suspendedRotation;
rotateAnimation.byValue = @(M_PI * 2);
[self.imageView.layer addAnimation:rotateAnimation forKey:@"rotate"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment