【问题标题】:CAAnimation with performSelector afterDelay带有 performSelector afterDelay 的 CAAnimation
【发布时间】:2011-11-17 02:56:54
【问题描述】:

我在搞乱一个基本的 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

注意:我在应用程序委托中使用等于[UIScreen mainScreen].applicationFrame 的框架实例化此视图。

【问题讨论】:

    标签: iphone uiview calayer caanimation performselector


    【解决方案1】:

    改变这个:

    [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 不会被激活。)

    以及为什么它可以毫不拖延地工作:因为只有当图层在树中并被显示时,隐式动画才会“启用”。

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多