如何在按下主页按钮后在后台为CABasicAnimation设置动画?

SBM*_*SBM 12 iphone objective-c ios

我是ios开发的新手.我正在我的项目中使用轮子图像.动画在前景模式下工作正常.之后,我按下主页按钮.现在我重新启动应用程序轮子动画不起作用.这是我的代码:

CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 1.0f;
animation.repeatCount = INFINITY;
[imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"];
Run Code Online (Sandbox Code Playgroud)

Jef*_*Sun 60

Swift 4.2更新

啊我明白了 - 使用这个,所有的情况,如进入后台后停止将被修复.

animation.isRemovedOnCompletion = false
Run Code Online (Sandbox Code Playgroud)


Rav*_*ran 2

尝试这个,

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addAnimation:) name:UIApplicationWillEnterForegroundNotification object:nil];

}

- (void)addAnimation:(NSNotification *)notificaiton
 {
 CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
 animation.fromValue = [NSNumber numberWithFloat: 2*M_PI];
 animation.toValue = [NSNumber numberWithFloat:0.0f];
 animation.duration = 4.0f;
 animation.repeatCount = INFINITY;
 [imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"];
 [imageRight.layer addAnimation:animation forKey:@"SpinAnimation"];
 }
Run Code Online (Sandbox Code Playgroud)