Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a CALayer-object from animationDidStop?

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]; 
}
like image 247
ragnarius Avatar asked Jun 13 '11 12:06

ragnarius


1 Answers

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];
     }
 }
like image 63
Nate Potter Avatar answered Sep 26 '22 07:09

Nate Potter