views:

412

answers:

1

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?

Thanks in advance for any answers!

+2  A: 

According to the documentation, you do it by creating an animation with an extremely large repeatCount (code excerpted from the documentation I linked to):

// 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 = 1e100f;

// 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 paragraph in the documentation:

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 invoking removeAnimationForKey:. You can stop all animations for a layer by sending the layer a removeAllAnimations message.

Brian Campbell
Contrary to the comment in the code sample you pasted, 1e100 is not infinite—it's just 10**100. The INFINITY constant, defined in math.h, would be better.
Peter Hosey
But if I do this, how do I stop the animation on command?
Responded in the answer since there isn't enough room in the comments.
Brian Campbell