gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:24:39 31 4
gpt4 key购买 nike

我正在尝试学习 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com