Question

I am messing around with a basic CAAnimation example where Im just trying to get a CALayer to rotate in a subclass of UIView. I start by making a new CALayer with a red background and inserting it into self.layer. I then make a CAAnimation that is supposed to be triggered on opacity changes and apply it to the CALayer, after which it spins as expected.

Weird thing is that if I try to apply the exact same animation using performSelector:withObject:afterDelay:, the CALayer no longer animates, although its opacity still changes. Even weirder is that if I use performSelector:withObject:afterDelay: to animate self.layer using pretty much the same code, it works.

Any ideas as to what's going on? Here is my code:

- (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;
}

and this is my interface

@interface MyView : UIView {
    CALayer *point;
}

-(void)test;
-(void)spinLayer:(CALayer*)layer;
-(CAAnimation*)animationForSpinning;

@end

Note: I am instantiating this view with a frame equal to [UIScreen mainScreen].applicationFrame from within the app delegate.

Was it helpful?

Solution

Change this:

[layer addAnimation:anim forKey:@"opacity"];

into this:

[layer addAnimation:anim
             forKey:@"notOpacity!"];

What happens is that implicit animation layer.opacity = 0.6 overwrites you explicit animation. This happens because Core Animation uses property names when adding implicit animations.

(If you move layer.opacity = 0.6 above [layer addAnimation:anim forKey:@"opacity"]; it will also work, but layer.opacity will not get animated.)

And why it works without delay: because implicit animations are "enabled" only if layer is in the tree and been displayed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top