I am trying to learn core-animation for the iOS/iPhone. My root layer contains a lot of sublayers (sprites) and thay should spin when they are removed...
My plan was to add a spinning animation and then remove the sprite when the animationDidStop is invoked. The problem is that the sprite layer is not a parameter to animationDidStop!
What is the best way to find the specific sprite layer from animationDidStop? Is there a better way to make the sprite spin when it is removed? (ideally I would like to use kCAOnOrderOut but I could not make it work)
-(void) eraseSprite:(CALayer*)spriteLayer {
CABasicAnimation* animSpin = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animSpin.toValue = [NSNumber numberWithFloat:2*M_PI];
animSpin.duration = 1;
animSpin.delegate = self;
[spriteLayer addAnimation:animSpin forKey:@"eraseAnimation"];
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
// TODO check if it is an eraseAnimation
// and find the spriteLayer
CALayer* spriteLayer = ??????
[spriteLayer removeFromSuperlayer];
}
Found this answer here cocoabuilder but basically you add a key value to the CABasicAnimation for CALayer that's being animated.
- (CABasicAnimation *)animationForLayer:(CALayer *)layer
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
/* animation properties */
[animation setValue:layer forKey:@"animationLayer"];
[animation setDelegate:self];
return animation;
}
Then reference it in the animationDidStop callback
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
CALayer *layer = [anim valueForKey:@"animationLayer"];
if (layer) {
NSLog(@"removed %@ (%@) from superview", layer, [layer name]);
[layer removeFromSuperlayer];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With