依次运行 CABasicAnimation
如何让一个 CABasicAnimation 在另一个 CABasicAnimation 完成后运行?换句话说,依次进行。我已经添加了第二个动画的开始时间,但是似乎第二个动画没有被执行:
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"];
知道为什么吗?
How can I have one CABasicAnimation to run after the other one has finishes? In other words sequentially. I've added the begin time of the second animation, however it seems that the second animation doesn't gets executed:
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"];
Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新了代码,这会起作用!
第一个动画
第二个动画:
}
这是第二个动画在之后触发的方式第一个完成。
Updated Code, this will work!
1st animation
2nd Animation:
}
This is how the 2nd animation would fire after the 1st one finishes.
最简单的事情是为第一个动画设置一个委托,并在该对象中实现
-animationDidStop:finished:
。在该方法中,触发第二个动画。The simplest thing to do is set up a delegate for the first animation and in that object implement
-animationDidStop:finished:
. In that method, touch off the second animation.您的动画未按计划运行的原因是您的编码方式和操作方式所致。使用核心动画。核心动画全部由 GPU 驱动。事实上,CA 可以很好地处理一些事情,以最佳方式渲染动画。
其中一个要素是现代显卡的原始并行处理能力,另一个要素是 Core Animation 可以将 CALayer 的内容缓存在卡上,这样您的代码就不需要不断地重绘它。顺便说一句,这也是 iPhone UI 在一些相对普通的硬件上速度如此之快的部分原因。核心动画可以自动利用多核 Mac,因为图层树是在单独的线程上渲染的。
最后一行是 imp。 CA 在单独的线程中运行(而不是主线程)。所以回到你的问题,你有 2 个 CA 块,每个块都试图同时为相同的
transform.translation.y
制作动画!这将如何运作?因此,要解决此问题,要么执行 - 正如
我只是想强调一下理论和原理。核心动画工作方式背后的推理。希望这有帮助...
The reason your animation in not running according to plan is due to the way you have coded up & used Core Animation. Core animations are all GPU powered. CA infact handles couple of things very well to render animations optimally.
One element of it is the raw parallel processing power of modern graphics cards, and another is the fact that Core Animation can cache the contents of a CALayer on the card so that your code doesn't need to constantly redraw it. This, by the way, is part of the reason the iPhone UI is so incredibly fast on some relatively modest hardware. Core Animation can automatically take advantage of a multi-core Mac because the layer tree is rendered on a separate thread.
The last line is imp. CA's are running in a separate thread ( & not the main thread). So coming back to your problem, you have 2 blocks of CA's each trying to animate same
transform.translation.y
Both at the same time! How would this work?So to solve this, either do -
I just wanted to highlight the theory & reasoning behind the way Core Animation works. Hope this helps...
真的就是这么简单......
您只需运行两个,但将第二个的 DELAY 设置为第一个的长度。
注意,在极少数复杂的情况下(您不知道第一个的长度),请使用
-animationDidStop:finished:
正如 @ChrisParker 指出的。不要忘记,要在 CA 动画中设置延迟,您只需使用这个愚蠢的代码:
这很奇怪,但这是执行顺序 CA 动画的唯一机制。
It's really this simple ...
You simply have to run both but set the DELAY on the second one to be the length of the first one.
NOTE THAT in rare complex cases (where you don't know the length of the first one), use
-animationDidStop:finished:
as @ChrisParker points out.Don't forget that to set a delay in CA animations, you just use this silly code:
It's bizarre but that's the only mechanism to do sequential CA animations.
iOS 15、Swift 5.x
这有点疯狂,但这是我可以开始工作的唯一解决方案,我尝试的其他所有东西都想并行执行。
请注意,当第一个完成时,它不会运行第二个,它会在运行第二个之前等待 4 秒,因为它知道第一个需要 4 秒。
iOS 15, Swift 5.x
It is a bit nuts, but it was the only solution I could get to work, everything else I tried wanted to do things in parallel.
Note this doesn't run the second when the first finishes, it waits 4 seconds before running the second since it knows the first takes 4 seconds.