0

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.

Eyal
  • 10,777
  • 18
  • 78
  • 130
  • 1
    possible duplicate of [Is there a way to pause a CABasicAnimation?](http://stackoverflow.com/questions/2306870/is-there-a-way-to-pause-a-cabasicanimation) – Manish Dubey Mar 26 '14 at 12:06

1 Answers1

0

You could gather all 'dynamic' behavior inside an update() method that would be called only when the app is in the foreground.

You would thus have methods for creating, updating and stopping your animations. When your application comes out of focus, the update is no longer called and the animation no longer updated. It still exists however and the associated variables are preserved in their previous states.

Tonio
  • 414
  • 5
  • 17