iBrad Apps iBrad Apps - 6 months ago 61
iOS Question

Multiple CAAnimations for animationDidStop method?

I know you have to use this method to get the delegate method for when the animation has finished:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {


The problem is, how would I distinguish between multiple CAAnimations like 2 or more?

I googled this and I haven't found anything useful.

Please share with me on how you accomplished this!

Thanks!

Answer

You can set key/value objects for CAAnimation instance like this:

CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation setValue:@"animation1" forKey:@"id"]; 
theAnimation.delegate = self;

CABasicAnimation *theAnimation2 = [CABasicAnimation animationWithKeyPath:@"opacity"];
[theAnimation2 setValue:@"animation2" forKey:@"id"];    
theAnimation2.delegate = self;

Check which one was called in delegate method:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if([[anim valueForKey:@"id"] isEqual:@"animation1"]) {
        NSLog(@"animation1");
    }
    if([[anim valueForKey:@"id"] isEqual:@"animation2"]) {
        NSLog(@"animation2");
    }
}