我正在处理一个基本的CAAnimation示例,在这个示例中,我只是想让一个CALayer在UIView的子类中旋转。我首先创建一个红色背景的新CALayer,并将其插入到self.layer中。然后,我创建了一个应该在不透明度更改时触发的CAAnimation,并将其应用于CALayer,之后它会按预期旋转。
奇怪的是,如果我尝试使用performSelector:withObject:afterDelay:应用完全相同的动画,CALayer将不再有动画效果,尽管它的不透明度仍在变化。更奇怪的是,如果我使用performSelector:withObject:afterDelay:来使用几乎相同的代码来动画self.layer,它就能工作。
你知道发生了什么事吗?下面是我的代码:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
point = [[CAGradientLayer alloc] init];
point.backgroundColor = [UIColor redColor].CGColor;
point.bounds = CGRectMake(0.0f, 0.0f, 30.0f, 20.0f);
point.position = CGPointMake(100.0f, 100.0f);
[self.layer addSublayer:point];
[point release];
[self performSelector:@selector(test) withObject:nil afterDelay:2];
}
return self;
}
-(void)test {
[self spinLayer:point];
// [self spinLayer:self.layer]; works here
}
-(CAAnimation*)animationForSpinning {
CATransform3D transform;
transform = CATransform3DMakeRotation(M_PI/2.0, 0, 0, 1.0);
CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
basicAnimation.toValue = [NSValue valueWithCATransform3D:transform];
basicAnimation.duration = 5;
basicAnimation.cumulative = NO;
basicAnimation.repeatCount = 10000;
return basicAnimation;
}
-(void)spinLayer:(CALayer*)layer {
CAAnimation *anim = [self animationForSpinning];
[layer addAnimation:anim forKey:@"opacity"];
layer.opacity = 0.6;
}这是我的界面
@interface MyView : UIView {
CALayer *point;
}
-(void)test;
-(void)spinLayer:(CALayer*)layer;
-(CAAnimation*)animationForSpinning;
@end注意:我在app委托中使用一个等于[UIScreen mainScreen].applicationFrame的框架来实例化这个视图。
发布于 2011-11-19 10:10:41
更改此设置:
[layer addAnimation:anim forKey:@"opacity"];如下所示:
[layer addAnimation:anim
forKey:@"notOpacity!"];实际情况是,隐式动画layer.opacity = 0.6会覆盖显式动画。这是因为Core Animation在添加隐式动画时使用了属性名称。
(如果将layer.opacity = 0.6移动到[layer addAnimation:anim forKey:@"opacity"];上方,它也会起作用,但layer.opacity不会设置动画。)
为什么它没有延迟地工作:因为只有当层在树中并被显示时,隐式动画才被“启用”。
https://stackoverflow.com/questions/8161546
复制相似问题