2009-04-24 165 views
7

为了尽可能简单地说出我的问题,有没有办法创建一个核心动画序列来重复一遍又一遍,直到停止?核心动画...循环动画?

具体来说,我正在做一个自定义类,我想要一个-start和-stop方法,它会导致它脉动。编写脉冲的动画代码不是问题,而是如何使其重复?

在此先感谢您的任何答案!

回答

15

the documentation,你做它用一个非常大的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; 

编辑:在OP问及如何停止动画。从文档中的next paragraph

您通过 发送addAnimation:forKey:消息 到目标层,传递 动画和标识符作为参数 开始的明确的动画。一旦添加到目标层 层,显式动画将运行 ,直到动画完成,或者从图层中删除 。用于将动画添加到 层的 标识符也用于通过 调用removeAnimationForKey:来停止它。您可以通过 发送图层 removeAllAnimations消息来停止某个图层的所有动画。

+3

与您粘贴的代码示例中的注释相反,1e100不是无限的 - 仅为10 ** 100。在math.h中定义的INFINITY常量会更好。 – 2009-04-24 03:20:28