views:

63

answers:

2

I have two animations in a custom UIView, anim1 and anim2. Anim1 sets its delegate to self and there is an animationDidStop method in my class which triggers Anim2. If I want something else to occur when Anim2 finishes, how do I do this? Can I specify a delegate method with a different name?

UPDATE

I declare two animations as iVars:

CABasicAnimation *topFlip;
CABasicAnimation *bottomFlip;

I build each animation and set delgate to self e.g.

- (CABasicAnimation *)bottomCharFlap: (CALayer *)charLayer
{


bottomFlip = [CABasicAnimation animationWithKeyPath:@"transform"]; 

charLayer.transform = CATransform3DMakeRotation(DegreesToRadians(0), 1, 0, 0); //set to end pos before animation

 bottomFlip.toValue      = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(DegreesToRadians(-360), 1, 0, 0)];
 bottomFlip.fromValue    = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(DegreesToRadians(-270), 1, 0, 0)]; 
 bottomFlip.autoreverses = NO; 
 bottomFlip.duration     = 0.5f;
 bottomFlip.repeatCount  = 1;
 bottomFlip.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseOut];
 bottomFlip.delegate = self;
 bottomFlip.removedOnCompletion = FALSE;



return  bottomFlip;
}

I then try and find bottomFlip in animationdidStop:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
if (theAnimation == bottomFlip) {
    NSLog(@"Bottom Animation is: %@", bottomFlip);
}
NSLog(@"Animation %@ stopped",theAnimation);


[bottomHalfCharLayerFront addAnimation:[self bottomCharFlap:bottomHalfCharLayerFront] forKey:@"bottomCharAnim"];
bottomHalfCharLayerFront.hidden = NO;
topHalfCharLayerFront.hidden = YES;


//insert the next one???
}

"Animation stopped" is logged but nothing else i.e. it doesn't seem to recognise the bottomFlip iVar

A: 

Just hold onto a reference to your animations as ivars and compare their memory addresses to the one that gets handed off to animationDidStop:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
  if (theAnimation == anim1)
  {
    // Spin off anim2
  }
  else
  {
    // anim2 stopped. Make something else occur
  }
}
Matt Long
Thanks! Could that be done with the 'forKey' part of addAnimation forKey too? Or is that only useful for removing animations?
codecowboy
I've added some code above. If I log the memory address of an animation after I've added it and then log it in animationDidStop, the memory addresses dont match. I think this is why your suggestion hasn't worked. if(theAnimation == anim1) never succeeds.
codecowboy
A: 

This seems to work:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//NSLog(@"First Animation stopped");

if (anim ==[topHalfCharLayerFront animationForKey:@"topCharAnim"]) {
    NSLog(@"Top Animation is: %@", anim);
    topHalfCharLayerFront.hidden = YES;
    [bottomHalfCharLayerFront addAnimation:[self bottomCharFlap:bottomHalfCharLayerFront] forKey:@"bottomCharAnim"];
    bottomHalfCharLayerFront.hidden = NO;
}

else if ((anim ==[bottomHalfCharLayerFront animationForKey:@"bottomCharAnim"])) {

    NSLog(@"Bottom Animation is: %@", anim);

}
codecowboy