xcode 核心动画...循环动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/784365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Core Animation... cyclic animations?
提问by cemulate
To phrase my question as simply as possible, is there a way to create a core animation sequence to repeat over and over until a stop?
尽可能简单地表达我的问题,有没有办法创建一个核心动画序列来一遍又一遍地重复直到停止?
Specifically, I'm making a custom class that I want to have a -start and -stop method that will cause it to pulsate. Writing the animation code for the pulse is not the problem, rather, how to make it repetitive?
具体来说,我正在制作一个自定义类,我想要一个 -start 和 -stop 方法来让它脉动。为脉冲编写动画代码不是问题,而是如何使其重复?
Thanks in advance for any answers!
提前感谢您的任何答案!
回答by Brian Campbell
According to the documentation, you do it by creating an animation with an extremely large repeatCount
(code excerpted from the documentation I linked to):
根据文档,您可以通过创建一个非常大的动画repeatCount
(从我链接到的文档中摘录的代码)来实现:
// create the animation that will handle the pulsing.
CABasicAnimation* pulseAnimation = [CABasicAnimation animation];
// over a one second duration, and run an infinite
// number of times
pulseAnimation.duration = 1.0;
pulseAnimation.repeatCount = HUGE_VALF;
// we want it to fade on, and fade off, so it needs to
// automatically autoreverse.. this causes the intensity
// input to go from 0 to 1 to 0
pulseAnimation.autoreverses = YES;
edit: The OP asked how to stop the animation. From the next paragraphin the documentation:
编辑:OP 询问如何停止动画。从文档中的下一段:
You start an explicit animation by sending a
addAnimation:forKey:
message to the target layer, passing the animation and an identifier as parameters. Once added to the target layer the explicit animation will run until the animation completes, or it is removed from the layer. The identifier used to add an animation to a layer is also used to stop it by invokingremoveAnimationForKey:
. You can stop all animations for a layer by sending the layer aremoveAllAnimations
message.
您可以通过向
addAnimation:forKey:
目标层发送消息、将动画和标识符作为参数传递来启动显式动画。一旦添加到目标层,显式动画将运行直到动画完成,或者从层中删除。用于向图层添加动画的标识符也用于通过调用removeAnimationForKey:
. 您可以通过向图层发送removeAllAnimations
消息来停止图层的所有动画 。