Search code examples
iosobjective-cbackgroundcabasicanimation

CABasicAnimation disappear when home button is pushed


I'm implementing a game, in which i have some CABasicAnimations. For example, like this :

CABasicAnimation * borddroit = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
borddroit.fromValue = [NSNumber numberWithFloat:0.0f];
borddroit.toValue = [NSNumber numberWithFloat:749.0f];
borddroit.duration = t;
borddroit.repeatCount = 1;
[ImageSuivante2.layer addAnimation:borddroit forKey:@"borddroit"];

I put it in pause with this function :

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

When my application is entering background, because the user push the home button, the animations correctly set in pause, but when i re-open my app, the animations have disappeard.

How could i fix it please ?

Thank you


Solution

  • This is correct and built-in behavior. When you leave the app, all animations are removed from their layers: the system calls removeAllAnimations on every layer.

    This doesn't matter, usually, for the following reason: suppose you animate a ball from point A to point B and it is halfway to point B in the animation when the user switches away from your app. When the user comes back, the animation is gone, but the ball is at point B, so the app can just continue. All that happens is that we've skipped some of the animation part.