作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试学习 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/
我是一名优秀的程序员,十分优秀!