Created
June 10, 2014 07:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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