我正在尝试学习iOS / iPhone的核心动画。我的根层包含很多子层(精灵),当它们被移除时它们应该旋转......
我的计划是添加旋转动画,然后在调用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];
}
答案 0 :(得分: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;
}
然后在animationDidStop回调中引用它
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
CALayer *layer = [anim valueForKey:@"animationLayer"];
if (layer) {
NSLog(@"removed %@ (%@) from superview", layer, [layer name]);
[layer removeFromSuperlayer];
}
}
答案 1 :(得分:0)
您可以拥有“CALayer”类型的 iVar iTempSpriteLayer
。
@property (nonautomic, assign) CALayer* iTempSpriteLayer;
-(void) eraseSprite:(CALayer*)spriteLayer {
iTempSpriteLayer = spriteLayer;
...........................
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
// TODO check if it is an eraseAnimation
// and find the spriteLayer
if(iTempSpriteLayer)
[iTempSpriteLayer removeFromSuperlayer];
iTempSpriteLayer = nil;
}