0

UIViewのサブクラスでCALayerを回転させようとしている基本的なCAAnimationの例をいじっています。まず、背景が赤の新しい 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注:アプリ デリゲート内の from に等しいフレームを使用して、このビューをインスタンス化しています。

4

1 に答える 1