0

I have 2 UIImageView's in the main view controller of my app. They are always animating via UIView animations. What I want to achieve is when the user clicks the home button, pause the UIView animations, and when the user comes back to the app, resume the animations.

So to pause I call a method from my app delegate to my ViewController to pause in the applicationWillResignActive call.

And to resume I call a method also from the app delegate to my ViewController to resume in the applicationDidBecomeActive method.

This is the code I use to either pause or resume my animations:

-(void)pauseLayer:(CALayer*)layer {
    CFTimeInterval paused_time = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = paused_time;
}

-(void)resumeLayer:(CALayer*)layer {
    CFTimeInterval paused_time = [layer timeOffset];
    layer.speed = 1.0f;
    layer.timeOffset = 0.0f;
    layer.beginTime = 0.0f;
    CFTimeInterval time_since_pause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - paused_time;
    layer.beginTime = time_since_pause;
}

- (void)pause:(BOOL)theBool {
    if (theBool) {
        [self pauseLayer:self.myImageView.layer];
        [self pauseLayer:self.myImageViewleft.layer];
    } else {
        [self resumeLayer:self.myImageView.layer];
        [self resumeLayer:self.myImageViewleft.layer];
    }
}

So I NSLogged everything and the methods get called accordingly however when I go to resume the animations, the image views don't resume from where they left off and are most likely at the position I placed them in the XIB (off the screen).

So when I re-enter my app, does my XIB get reloaded or something? There is absolutely nothing that should be changing the position of those UIImageView's, thats why I am really confused why this is happening.

Edit: I have also tried creating my images programmatically, no luck same issue. The pausing and resuming works successfully if I do it via buttons in my app while its running, it seems the problem is with the application delegate methods or something.

If anyone sees what's wrong, please let me know!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

1 Answers1

2

I found this to be really complicated and tricky so for now I will just use Cocos2D instead and make use of CCDirector's pause feature.

Edit: Just tried CCDirector's pause feature and it works perfectly. Definitely go this route!!!

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191