如何从animationDidStop中删除CALayer对象?

rag*_*ius 7 iphone animation core-animation ios

我正在尝试学习iOS/iPhone的核心动画.我的根层包含很多子层(sprite),当它们被移除时它们会旋转...

我的计划是添加旋转动画,然后在调用animationDidStop时删除精灵.问题是sprite图层不是animationDidStop的参数!

从animationDidStop中找到特定sprite图层的最佳方法是什么?是否有一种更好的方法可以在精灵被移除时使其旋转?(理想情况下,我想使用kCAOnOrderOut,但我无法使其工作)

-(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]; 
}
Run Code Online (Sandbox Code Playgroud)

Nat*_*ter 24

cocoabuilder中找到了这个答案,但基本上你将一个键值添加到动画的CALayer的CABasicAnimation中.

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

然后在animationDidStop回调中引用它

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
 {
     CALayer *layer = [anim valueForKey:@"animationLayer"];
     if (layer) {
         NSLog(@"removed %@ (%@) from superview", layer, [layer name]);
         [layer removeFromSuperlayer];
     }
 }
Run Code Online (Sandbox Code Playgroud)