如何从animationDidStop中删除CALayer对象? [英] How to remove a CALayer-object from animationDidStop?

查看:186
本文介绍了如何从animationDidStop中删除CALayer对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习iOS / iPhone的核心动画。我的根层包含许多子层(子图形),并且在移除子层时应该旋转……

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...

我的计划是添加旋转动画,然后删除子图形调用animationDidStop时。问题在于子画面图层不是animationDidStop的参数!

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!

从animationDidStop查找特定子画面图层的最佳方法是什么?
有没有更好的方法可以使精灵在删除时旋转? (理想情况下,我想使用kCAOnOrderOut,但我无法使其工作)

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]; 
}


推荐答案

在这里找到了这个答案 cocoabuilder ,但基本上,您可以向CABasicAnimation添加键值

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;
}

然后在animationDidStop回调中引用它

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];
     }
 }

这篇关于如何从animationDidStop中删除CALayer对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆