Iphone 带performSelector afterDelay的CAAnimation
我正在处理一个基本的CAAnimation示例,我只是想让CALayer在UIView的子类中旋转。我首先制作一个红色背景的新CALayer,并将其插入self.layer。然后我制作一个动画,该动画应该在不透明度更改时触发,并将其应用于CALayer,之后它会按预期旋转 奇怪的是,如果我尝试使用performSelector:withObject:afterDelay:应用完全相同的动画,CALayer将不再动画,尽管其不透明度仍然会更改。更奇怪的是,如果我使用performSelector:withObject:afterDelay:使用几乎相同的代码为self.layer设置动画,它就可以工作了 你知道发生了什么事吗?这是我的密码:Iphone 带performSelector afterDelay的CAAnimation,iphone,uiview,calayer,caanimation,performselector,Iphone,Uiview,Calayer,Caanimation,Performselector,我正在处理一个基本的CAAnimation示例,我只是想让CALayer在UIView的子类中旋转。我首先制作一个红色背景的新CALayer,并将其插入self.layer。然后我制作一个动画,该动画应该在不透明度更改时触发,并将其应用于CALayer,之后它会按预期旋转 奇怪的是,如果我尝试使用performSelector:withObject:afterDelay:应用完全相同的动画,CALayer将不再动画,尽管其不透明度仍然会更改。更奇怪的是,如果我使用performSelector:
- (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
的框架实例化此视图。更改此:
[layer addAnimation:anim forKey:@"opacity"];
为此:
[layer addAnimation:anim
forKey:@"notOpacity!"];
发生的情况是隐式动画layer.opacity=0.6
覆盖显式动画。这是因为核心动画在添加隐式动画时使用属性名称
(如果将layer.opacity=0.6
移动到[layer addAnimation:anim forKey:@“opacity”];
也可以,但layer.opacity
不会设置动画。)
为什么它可以毫不延迟地工作:因为隐式动画只有在图层位于树中并已显示时才会“启用”