Proud Member
Proud Member

Reputation: 40496

How to reuse an CABasicAnimation when not removed after completion?

Lots of people are talking about keeping an CABasicAnimation object around after it has been used.

So by setting

removedOnCompletion = NO

the animation object keeps attached to the layer when the animation is complete. How would I re-launch this animation again without creating a new CABasicAnimation?

What's the point of keeping this object around? The only benefit I know is that we can set removedOnCompletion = NO and set kCAFillModeForwards so that Core Animation does not revert the visual representation back to the model values in the CALayer.

But how to re-use the animation, like everyone is talking about?

Upvotes: 7

Views: 3251

Answers (2)

Martin Wickman
Martin Wickman

Reputation: 19905

Try this:

  1. Add the animation with a key using [layer addAnimation:myAnimation forKey:@"myKey"]

  2. To get a reference to it later, call [layer animationForKey:@"myKey"]

  3. Read this note from Apple which explains how to pause and resume (restart) animations.

Upvotes: 6

r farnell
r farnell

Reputation: 71

This worked for me but I am sure someone will tell me the value is already stored in a key..

When I created a layer and assigned an animation to it I set the animation to a keyed value associated with the layer.

in this example layersCurrentAnimation is the CABasicAnimation and myAnimatedLayer is a CALayer.

[myAnimatedLayer setValue:layersCurrentAnimation forKey:@"basicAnimation"]

then if the animation was to stop I would get the animation value out from the layer and re-run it

CABasicAnimation *thisAnimation = [myAnimatedLayer  valueForKey:@"basicAnimation"];

[myAnimatedLayer addAnimation:thisAnimation forKey:nil];

Upvotes: 0

Related Questions