17

我是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

6 回答 6

80

斯威夫特 4.2 更新

啊,我想通了 - 使用它,所有的情况,比如进入后台后停止,都将得到修复。

animation.isRemovedOnCompletion = false
于 2014-11-22T05:31:52.980 回答
4

尝试这个,

- (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"];
 }
于 2013-06-17T14:04:15.523 回答
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"];
 }
于 2013-06-17T14:05:56.687 回答
2

当您离开应用程序时,所有动画都会从它们的图层中删除:系统会在每个图层上调用 removeAllAnimations。所以如果你想继续动画,那么你可以听 UIApplicationDidBecomeActiveNotification 并重新开始动画。

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (![_imageLeft.layer animationForKey:@"SpinAnimation"])
    {
         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"];
    }


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addAnimation:) name:UIApplicationDidBecomeActiveNotification object:nil];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)addAnimation:(NSNotification *)notificaiton
{
   if (_imageLeft && ![_imageLeft.layer animationForKey:@"SpinAnimation"])
   {
       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"];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
于 2013-06-17T11:25:44.147 回答
2

通常,它的编码方式如下:

        animation.fillMode = CAMediaTimingFillMode.forwards
        animation.isRemovedOnCompletion = false

但是,如果它没有按预期工作,您可能会错过一些代码:

        animation.beginTime = CACurrentMediaTime()
于 2019-07-18T12:09:03.207 回答
1

当应用程序进入后台时,系统会从其图层中删除所有动画。在您的viewWillAppear:方法中,注册UIApplicationDidBecomeActiveNotification. 当您观察通知时,再次添加动画。在 中取消注册通知viewWillDisappear:

于 2013-06-17T17:12:49.803 回答