1

我是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"];
4

2 回答 2

3

尝试这个,

- (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"];
 }
//-----------------------------------------------------------------------------

//How to remove an observer for NSNotification

      [[NSNotificationCenter defaultCenter] removeObserver:self];
于 2013-06-17T14:05:21.983 回答
0

当您的应用程序暂停(发送到后台)时,所有动画和计时器都会停止。这是为了节省电池。当您的应用程序被带到前台时,您必须自己再次启动动画。

您可以- (void)applicationDidBecomeActive:(UIApplication *)application为此在应用程序中委托我们。

或者在保存动画的视图控制器中监听UIApplicationDidBecomeActiveNotification通知。

于 2013-06-17T08:07:37.593 回答