CALayer: callback when animation ends

animationcabasicanimationcalayercore-animation

I have been running into some issues with animating multiple CALayers at the same time, and was hoping someone could point me in the right direction.

My app contains an array of CALayer. The position of each layer is set to (previousLayer.position.y + previousLayer.bounds.height), which basically lays them out similar to a table. I then have a method that, every-time it is called, adds a new layer to the stack and sets its Y position is set to 0. The Y positions of all other layers in the array are then offset by the height of the new layer (essentially pushing all old layers down).

What I am having problems with is preventing the adding of new layers until the previous animation has completed. Is there a way to tell when an implicit animation has finished? Or alternatively, if I use CABasicAnimation and animationDidFinish, is there a way to tell which object finished animating when animationDidFinish is called?

Best Answer

You can set arbitrary values for keys on your animation object. What this means is that you can associate your layer that you're animating with the animation and then query it in -animationDidStop:finished: You create your animation this way:

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
// set other fields...

[anim setValue:layerToAnimate forKey:@"layer"];

// Start the animation
[layerToAnimate addAnimation:anim forKey:nil];

Then check for that value when the animation stops:

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
    CALayer *animatedLayer = [animation valueForKey:@"layer"];
    // do something with the layer based on some condition...
    // spin off the next animation...

    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue
                 forKey:kCATransactionDisableActions];
    [animatedLayer setPosition:position];
    [CATransaction commit];
}

This is explicit animation, but it should give you what you need.

Related Topic