iphone - 如何从 animationDidStop 中删除 CALayer 对象?

标签 iphone ios animation core-animation

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

我的计划是添加一个旋转动画,然后在调用 animationDidStop 时移除 Sprite 。问题是 Sprite 层不是 animationDidStop 的参数!

从 animationDidStop 中找到特定 Sprite 层的最佳方法是什么? 有没有更好的方法让 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]; 
}

最佳答案

在这里找到这个答案 cocoabuilder但基本上你为正在动画的 CALayer 添加一个键值到 CABasicAnimation。

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

然后在animationDidStop回调中引用它

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

关于iphone - 如何从 animationDidStop 中删除 CALayer 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6330701/

相关文章:

iphone - 当我尝试 +2.50 时,NSString 四舍五入到最接近的整数

animation - Threejs Mixer 未更新为 `setTime`

第二次点击时没有发生 Jquery 动画

css - 模块化进度条

iPhone:如何阻止App(或UIWebView)访问网络/web

ios - 如何将 fork 存储库添加到 CocoaPod 的 .podspec 文件中的 "s.dependency"属性?

iphone - 如何比较两个 NSObject 的结构/属性的相等性

ios - 如何在 collectionView 单元格中设置 NSLayoutConstraint 常量?

iOS UIWebView 限制

ios - Xcode 6 : Error copying template database