CAKeyFrameAnimation 中的 keyTime 是什么样的值?

发布于 2024-08-20 03:57:09 字数 1986 浏览 3 评论 0原文

例如,我有这个 CAKeyFrameAnimation:

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

    animation.duration = 1.6;
    //animation.cumulative = YES;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;

    animation.values = [NSArray arrayWithObjects:
                        [NSNumber numberWithFloat:0.0 * M_PI],
                        [NSNumber numberWithFloat:(15.0/180.0) * M_PI],
                        [NSNumber numberWithFloat:(30.0/180.0) * M_PI], // animation stops here...
                        [NSNumber numberWithFloat:(45.0/180.0) * M_PI], // ignored!
                        [NSNumber numberWithFloat:(190.0/180.0) * M_PI], nil]; // ignored!

    animation.keyTimes = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0],
                          [NSNumber numberWithFloat:0.2],
                          [NSNumber numberWithFloat:0.4], // ignored!
                          [NSNumber numberWithFloat:0.8], // ignored!
                          [NSNumber numberWithFloat:1.6], 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"];

我不明白的是:

A)关键时间值是自动画开始以来的绝对经过时间吗?

B) 关键时间值只是说明该特定关键帧要使用多少时间?

For example I have this CAKeyFrameAnimation:

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

    animation.duration = 1.6;
    //animation.cumulative = YES;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;

    animation.values = [NSArray arrayWithObjects:
                        [NSNumber numberWithFloat:0.0 * M_PI],
                        [NSNumber numberWithFloat:(15.0/180.0) * M_PI],
                        [NSNumber numberWithFloat:(30.0/180.0) * M_PI], // animation stops here...
                        [NSNumber numberWithFloat:(45.0/180.0) * M_PI], // ignored!
                        [NSNumber numberWithFloat:(190.0/180.0) * M_PI], nil]; // ignored!

    animation.keyTimes = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0],
                          [NSNumber numberWithFloat:0.2],
                          [NSNumber numberWithFloat:0.4], // ignored!
                          [NSNumber numberWithFloat:0.8], // ignored!
                          [NSNumber numberWithFloat:1.6], 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"];

What I don't get is:

A) are key time values absolute passed time since the animation has started?

B) are key time values just saying how much time to use for this particular key frame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿看小鸭子会跑 2024-08-27 03:57:09

docs 这里的措辞有点奇怪,但很准确:

数组中的每个值都是 0.0 到 1.0 之间的浮点数,对应于值数组中的一个元素。 keyTimes 数组中的每个元素将相应关键帧值的持续时间定义为动画总持续时间的一部分。每个元素值必须大于或等于前一个值。

基本上,每个值都指示给定关键帧发生在动画中的哪个标准化点。因此,如果关键帧占动画的 25%,则该值将为 0.25。文档中令人困惑的部分是它们表明这是一个持续时间,而实际上它是一个标准化的时间点。

The docs are phrased a little oddly here, but are accurate:

Each value in the array is a floating point number between 0.0 and 1.0 and corresponds to one element in the values array. Each element in the keyTimes array defines the duration of the corresponding keyframe value as a fraction of the total duration of the animation. Each element value must be greater than, or equal to, the previous value.

Basically, each value indicates at what normalized point in the animation the given keyframe occurs. So if a keyframe is 25% into the animation, the value would be 0.25. The confusing part of the docs is they indicate that it is a duration, when in fact it's a normalized point in time.

怪我鬧 2024-08-27 03:57:09

keyTimes 的值是总持续时间的百分比。有效值范围为 0 到 1(0% 到 100%)。您最后的值 1.6 无效。

举个例子,如果一个关键帧应该发生在动画的 0.8 秒处,那么在持续时间为 1.6 秒的情况下,keyTime 将为 0.5。

The value of keyTimes is a percent of the total duration. Valid values range between 0 and 1 (0% to 100%). Your last value of 1.6 is invalid.

As an example, if a keyframe is supposed to happen 0.8 seconds into the animation that keyTime would be 0.5 given your duration of 1.6 seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文