首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按顺序运行CABasicAnimation

按顺序运行CABasicAnimation
EN

Stack Overflow用户
提问于 2011-10-17 08:18:02
回答 4查看 17.7K关注 0票数 10

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

代码语言:javascript
复制
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"];

知道为什么吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-10-17 09:27:27

已更新代码,这将会起作用!

动画

代码语言:javascript
复制
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"];

第二部动画:

代码语言:javascript
复制
    - (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"];
}

}

这就是在第一个动画结束后,第二个动画将如何触发。

票数 15
EN

Stack Overflow用户

发布于 2011-10-17 08:24:46

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

票数 1
EN

Stack Overflow用户

发布于 2011-10-17 10:56:22

你的动画没有按照计划运行的原因是由于你编码的方式&使用核心动画。核心动画都是由GPU驱动的。事实上,CA很好地处理了一些事情,以便以最佳方式呈现动画。

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

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

所以要解决这个问题,要么-

  1. ,有人建议,将第二个动画延迟,这样在第一个动画之后,第二个动画只在第一个操作系统结束后生效。
  2. 或尝试在一个块中完成整个动画(也是有人建议的)。

我只想强调核心动画工作方式背后的理论和推理。希望这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7788289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档