I have a button with a background image, when the app is running I want the button to rotate, and when the app goes to background the button should stop rotating.
I call the method startSpiningAnimation
that rotate the button infinitely.
when the app goes to background I call stopSpinningAnimation
to stop the rotate animation.
- (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];
}
Everything works great the only issue I have is that every time the app comes to foreground and the animation starts, it starts from original position.
(I completely understand why it happens, obesely I'm removing all animation when app goes to background.. I just dont know how to fix that)
What I want is that the button rotation animation will continue from the last angle it was.