主页button被按下时,CABasicAnimation消失

我正在实施一个游戏,其中我有一些CABasicAnimations。 例如,像这样:

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"]; 

我用这个函数暂停:

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

当我的应用程序进入后台,因为用户按下home键,animation正确设置暂停,但是当我重新打开我的应用程序,animation已经消失。

我怎么能修好它?

谢谢

这是正确的和内置的行为。 当你离开应用程序时,所有animation都会从其图层中删除:系统会调用每个图层的removeAllAnimations

这通常是无关紧要的,原因如下:假设你从A点移动到B点,当用户离开你的应用程序时,B点位于animation的中点。 当用户回来时,animation消失了,但是球 B点,所以应用程序可以继续。 发生的只是我们跳过了一些animation部分。

观察UIApplicationWillEnterForegroundNotification并重新创buildCAAnimation

只需在UIViewControllerUIView类中添加下面的代码即可。 无需恢复或重新开始animation。 它将由自己处​​理:)

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive:) name:UIApplicationWillEnterForegroundNotification object:nil]; -(void)becomeActive : (NSNotification*)notification { NSLog(@"app become active"); }