views:

77

answers:

1

I'm using following lines of code to animate:

CATransition *animation = [self getAnimation:dirString];
    [[self view] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    [[[self view] layer] addAnimation:animation forKey:kAnimationKey];

After end of animation, I want to play sound. How can I get notification when animation is completed?

A: 

CATransition is a subclass of CAAnimation, so it inherits (among other things) a delegate property, and a delegate method called animationDidStop:finished:. Define that method in whatever class you'd like to handle the event, and set the delegate property of the animation to an object of that class. When the animation finishes, the animationDidStop:finished: message is sent to the delegate.

For example:

AnimationDelegate.m
@implementation AnimationDelegate

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
     //Handle whatever needs to be done after the animation stops
}

ClassUsingAnimation.m
@implementation ClassUsingAnimation
{
    AnimationDelegate *customDelegate = [[AnimationDelegate alloc] init];
    CATransition *animation = [self getAnimation:dirString];
      animation.delegate = customDelegate;
      [[self view] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
      [[[self view] layer] addAnimation:animation forKey:kAnimationKey];
    [customDelegate release];
}
Endemic