Search code examples
iphoneeventsanimationcore-animation

iPhone - Catching the end of a grouped animation


I animate 2 views, each one with its CAAnimationGroup that contains 2 CAAnimations. They are launched at the same time (if computing time is negligible), and have the same duration.

How may I do to know when both grouped animation is finished.

I put the - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag delegate method, but... What may I test ? Sounds simple, but I don't see the way of doing this.


Solution

  • You can use two variables to track whether the animations have completed:

    BOOL firstAnimFinished;
    BOOL secondAnimFinished;
    

    then in your animationDidStop delegate you check which animation is calling the method and set the flags appropriately. The catch is that you'll need to add a key to identify the animations when they call the delegate (the animations you created will not be the ones calling the delegate, which is a subject for another question/rant). For example:

    // when you create the animations
    [firstAnmim setValue: @"FirstAnim" ForKey: @"Name"];
    [secondAnmim setValue: @"SecondAnim" ForKey: @"Name"];
    
    // Your delegate
    - (void)animationDidStop:(CAAnimation*)theAnimation finished:(BOOL)flag {
        NSString* name = [theAnimation valueForKey: @"Name"];
        if ([name isEqualToString: @"FirstAnim"]) {
            firstAnimFinished = YES;
        } else if ([name isEqualToString: @"SecondAnim"]) {
            secondAnimFinished = YES;
        }
        if (firstAnimFinished && secondAnimFinished) {
            // ALL DONE...
        }
    }