问题
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
回答1:
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.
回答2:
Observe UIApplicationWillEnterForegroundNotification
and re-animate CAAnimation
回答3:
Just add below code in either your UIViewController
or in your UIView
class. No need to resume or start again animation. It will handle by its own :)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive:) name:UIApplicationWillEnterForegroundNotification object:nil];
-(void)becomeActive : (NSNotification*)notification
{
NSLog(@"app become active");
}
来源:https://stackoverflow.com/questions/15852554/cabasicanimation-disappear-when-home-button-is-pushed