0

我尝试做一个 CAKeyFrameAnimation 来旋转图层:

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"];

就像您在评论中看到的那样,动画只运行两个关键帧,但不是全部。无论我在那里输入什么样的值,动画都不会运行超过两个关键帧。

那里可能有什么问题?

4

2 回答 2

4

可能是您将 animation.duration 设置为 1,但您的 keyTimes 是 0、0.2,然后是 2... 这意味着动画将在达到您的第 3 个值之前停止。

于 2010-02-02T13:46:07.830 回答
1

keyTimes 数组只能包含从 0.0 到 1.0 的递增值。这些是动画进度的百分比,而不是以秒为单位的原始数字。因此,与 keyTime 0.5 匹配的关键帧将发生在动画的一半,而不是动画的 1/2 秒(当然,除非你有一个一秒的动画)。

于 2010-08-10T15:57:50.867 回答