Iphone 按顺序运行CABASICIATION

Iphone 按顺序运行CABASICIATION,iphone,objective-c,ios,core-animation,cabasicanimation,Iphone,Objective C,Ios,Core Animation,Cabasicanimation,在另一个完成后,我如何运行一个CABASICIATION?换句话说,按顺序。我添加了第二个动画的开始时间,但是第二个动画似乎没有执行: CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"]; appearance.duration = 0.5; appearance.fromValue = [NSNumber numberWithFloat

在另一个完成后,我如何运行一个CABASICIATION?换句话说,按顺序。我添加了第二个动画的开始时间,但是第二个动画似乎没有执行:

CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    appearance.duration = 0.5;
    appearance.fromValue = [NSNumber numberWithFloat:0];
    appearance.toValue = [NSNumber numberWithFloat:340];
    appearance.repeatCount = 1;
    appearance.fillMode = kCAFillModeForwards;
    appearance.removedOnCompletion = NO;
    [notif.layer addAnimation:appearance forKey:@"transform.translation.y"];



    CABasicAnimation * theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    theAnimation.duration = 0.5;
    theAnimation.fromValue = [NSNumber numberWithFloat:0];
    theAnimation.toValue = [NSNumber numberWithFloat:10];
    theAnimation.repeatCount = 3;
    theAnimation.autoreverses = YES;
    theAnimation.fillMode = kCAFillModeForwards;
    theAnimation.removedOnCompletion = NO;
    theAnimation.beginTime = appearance.beginTime + appearance.duration;
    [notif.layer addAnimation:theAnimation forKey:@"transform.translation.y"];

知道为什么吗?

最简单的方法是为第一个动画设置一个代理,并在该对象中实现
-animationDidStop:finished:
。在该方法中,触发第二个动画。

更新的代码,这将起作用

第一部动画

CABasicAnimation * appearance =[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    [appearance setValue:@"animation1" forKey:@"id"];
    appearance.delegate = self;
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
    appearance.duration = 0.5;
    appearance.fromValue = [NSNumber numberWithFloat:0];
    appearance.toValue = [NSNumber numberWithFloat:340];
    appearance.repeatCount = 1;
    appearance.fillMode = kCAFillModeForwards;
    appearance.removedOnCompletion = NO;
    [notif.layer addAnimation:appearance forKey:@"transform.translation.y"];
第二部动画:

    - (void)animationDidStop:(CAAnimation *)theAnimation2 finished:(BOOL)flag {
    if([[theAnimation2 valueForKey:@"id"] isEqual:@"animation1"]) {
    CABasicAnimation * theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    theAnimation.duration = 0.5;
    theAnimation.fromValue = [NSNumber numberWithFloat:0];
    theAnimation.toValue = [NSNumber numberWithFloat:10];
    theAnimation.repeatCount = 3;
    theAnimation.autoreverses = YES;
    theAnimation.fillMode = kCAFillModeForwards;
    theAnimation.removedOnCompletion = NO;
    theAnimation.beginTime = appearance.beginTime + appearance.duration;
    [notif.layer addAnimation:theAnimation forKey:@"transform.translation.y"];
}
}


这就是第二个动画在第一个动画完成后的触发方式。

您的动画未按计划运行的原因是您编写和使用核心动画的方式。核心动画都是GPU驱动的。CA事实上可以很好地处理两件事情,以最佳方式渲染动画

它的一个要素是现代图形卡的原始并行处理能力,另一个要素是核心动画可以缓存卡上CALayer的内容,这样代码就不需要不断地重新绘制。顺便说一句,这也是iPhone用户界面在一些相对普通的硬件上速度如此之快的部分原因。核心动画可以自动利用多核心Mac,因为层树是在一个单独的线程上渲染的

最后一行是imp.CA在单独的线程(而不是主线程)中运行。回到你的问题上来,你有两个CA块,每个CA块都试图同时为相同的
transform.translation.y
设置动画!这是怎么回事

所以要解决这个问题,你也可以-

  • 有人建议,为第二个动画设置延迟,以便在第一个动画完成后,第二个动画仅在第一个操作系统结束后启动
  • 或者尝试在一个块中完成整个动画(正如有人建议的那样)

  • 我只是想强调核心动画工作方式背后的理论和推理。希望这有帮助……

    最简单、最干净的方法是使用
    CAAnimationGroup
    。看看这个答案,它帮助了我


    这不起作用。。animationDidStop被称为Infinitely如果你有一个循环,你确定你没有设置第二个动画的代理,使第二个动画也调用animationDidStop吗?如果你查看它自己的代码,它不会编译。。您正在声明一个变量theAnimation,其中函数参数名也是theAnimation哎呀,我在上面修复了它,不使用Xcode时很难看到它!:/另外,如果你喜欢的话,也请投票支持我的答案:现在是2016年。这仍然是按顺序执行动画的最佳解决方案吗?
    CAAnimationGroup
    用于并发动画,而不是顺序动画,通常用于设置多个属性的动画。