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

时间:2013-06-17 07:26:58

标签: iphone ios objective-c

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

6 个答案:

答案 0 :(得分:55)

Swift 4.2更新

啊我想出来了 - 使用这个,所有的情况,如进入后台后停止将被修复。

animation.isRemovedOnCompletion = false

答案 1 :(得分: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"];
 }

答案 2 :(得分: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];
}

答案 3 :(得分: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"];
 }

答案 4 :(得分:1)

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

答案 5 :(得分:0)

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

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

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

        animation.beginTime = CACurrentMediaTime()