Search code examples
iphonecore-animationcakeyframeanimation

Why are my animation values ignored?


I try to do a CAKeyFrameAnimation for rotating an layer:

CALayer* theLayer = myView.layer;
    CAKeyframeAnimation* animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];

    animation.duration = 1.0;
    animation.cumulative = NO;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;

    animation.values = [NSArray arrayWithObjects:
                        [NSNumber numberWithFloat:0.0 * M_PI],
                        [NSNumber numberWithFloat:0.5 * M_PI],
                        [NSNumber numberWithFloat:0.3 * M_PI], // animation stops here...
                        [NSNumber numberWithFloat:0.8 * M_PI], // ignored!
                        [NSNumber numberWithFloat:0.7 * M_PI], nil]; // ignored!

    animation.keyTimes = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0],
                          [NSNumber numberWithFloat:0.2],
                          [NSNumber numberWithFloat:2.0], // ignored!
                          [NSNumber numberWithFloat:1.5], // ignored!
                          [NSNumber numberWithFloat:2], nil]; // ignored!

    animation.timingFunctions = [NSArray arrayWithObjects:
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], 
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear], nil];

    [theLayer addAnimation:animation forKey:@"transform.rotation.z"];

Like you can see in the comments, the animation only runs two of the key frames, but not all of them. No matter what kind of values I put in there, the animation will never run more than two key frames.

What could be wrong there?


Solution

  • Could it be that you set the animation.duration to 1 and yet your keyTimes are 0, 0.2 and then 2... meaning that the animation will stop before it reaches your 3rd value.