Search code examples
ioscore-animation

Can I reuse a CAAnimationGroup by returning it from a layer's animationForKey:?


I'm trying to do something like:

<Create a CAAnimationGroup aGroup with removedOnCompletion = NO ...>
[aLayer addAnimation:aGroup forKey:@"someKey"];

So the animation group plays...

And then I'd want to retrieve the same animation group without recreating it and re-add it to the layer so that it replays. Something like:

CAAnimationGroup* theOldGroup = (CAAnimationGroup*)[aLayer animationForKey:@"someKey"];
[aLayer addAnimation:theOldGroup forKey:@"someKey"];

However, the address of aGroup is different than the one I get back from animationForKey. And trying to set its speed to 1 or doing anything with it crashes the app (aka BAD_ACCESS)

What am I missing?


Solution

  • No, you can't reuse them the way you are describing in your question.


    The documentation for animationForKey: says you can't do it this way (in the discussion):

    animationForKey:

    Returns the animation added to the receiver with the specified identifier.

    - (CAAnimation *)animationForKey:(NSString *)key

    Parameters

    key
    A string that specifies the identifier of the animation.

    Return Value

    The animation object matching the identifier, or nil if no such animation exists.

    Discussion

    Attempting to modify any properties of the returned object will result in undefined behavior.