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 = [CAMediaTimingF开发者_开发技巧unction 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
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);
}
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
}
}
精彩评论