How to remove a CALayer object from animationDidStop?

I am trying to learn core-animation for iOS / iPhone. My root layer contains many sublayers (sprites), and thay should rotate when they are removed ...

My plan was to add spinning animation and then remove the sprite when calling animationDidStop. The problem is that the sprite layer is not a parameter for the DidStop animation!

What is the best way to find a specific sprite layer from animDidStop? Is there a better way to do a sprite spin when it is removed? (Ideally, I would like to use kCAOnOrderOut, but I could not get it to 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]; } 
+7
source share
2 answers

Found this answer here cocoabuilder , but basically you add the key value for CABasicAnimation for CALayer, which is being animated.

 - (CABasicAnimation *)animationForLayer:(CALayer *)layer { CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; /* animation properties */ [animation setValue:layer forKey:@"animationLayer"]; [animation setDelegate:self]; return animation; } 

Then specify 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]; } } 
+23
source

You may have an iVar iTempSpriteLayer type `CALayer.

 @property (nonautomic, assign) CALayer* iTempSpriteLayer; 

 -(void) eraseSprite:(CALayer*)spriteLayer { iTempSpriteLayer = spriteLayer; ........................... } - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ // TODO check if it is an eraseAnimation // and find the spriteLayer if(iTempSpriteLayer) [iTempSpriteLayer removeFromSuperlayer]; iTempSpriteLayer = nil; } 
0
source

All Articles