CAKeyframeAnimation 中的关键帧是否总是准确命中?

发布于 2024-11-08 14:03:30 字数 324 浏览 0 评论 0原文

谁能告诉我当动画运行时,CAKeyframeAnimation 中的关键帧是否始终保证以其精确值命中?或者...它们仅充当插值指南?例如,如果我指定路径上的 3 个点供某个任意属性遵循 - 让我们称之为“位置” - 并且我指定 0.3f 秒的执行时间,而(显然)点 1 和 3 必须被命中(如它们是终点)我可以保证点 2 将完全按照关键帧数组中的指定进行评估吗?令人惊讶的是,我没有找到任何一份文件可以给出足够的答案。我问这个问题是因为我正在编写一个 OpenAL 音效同步器,它使用关键帧动画的路径来触发沿其长度的各种短声音,虽然大多数声音都会被执行,但偶尔有一些不会执行,我也不知道如果是我的逻辑错误或者我的代码错误。 提前致谢。

Can anyone tell me if the key frames in a CAKeyframeAnimation are always guaranteed to be hit with their exact values when the animation runs? Or... do they only act as interpolation guides? e.g. If I specify, say, 3 points on a path for some arbitrary property to follow - let's call it 'position' - and I specify an execution time of 0.3f seconds, whilst (obviously) points 1 and 3 must be hit (as they are the terminal points) can I guarantee that point 2 will be evaluated exactly as specified in the key frame array? Surprisingly, I haven't found a single document that gives an adequate answer. I ask this because I'm writing an OpenAL sound-effect synchroniser that uses a keyframe animation's path to trigger various short sounds along its length and whilst most of them get executed, now and again a few don't and I don't know if it's my logic that's wrong or my code.
Thanks in advance.

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

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

发布评论

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

评论(1

德意的啸 2024-11-15 14:03:30

一般来说,依赖作为计算结果的浮点值的“准确性”是充满危险的。例如,以下代码:

CGFloat x1 = some_function();
CGFloat x2 = some_other_function();
if(x1 == x2)
{
    // do something
}

甚至不知道函数的作用很可能是错误的。即使这些函数执行非常相似的计算,优化器也可能会重新排序操作,从而导致足以导致相等测试失败的小舍入误差。

它应该是:

CGFloat x1 = some_function();
CGFloat x2 = some_other_function();
CGFloat tolerance = 0.1; // or some tolerance suitable for the calculation.
if(fabsf(x1 - x2) < tolerance)
{
    // do something
}

其中公差是适合正在执行的计算的某个值。

因此,在不了解 CAKeyframeAnimation 内部结构的情况下,我可以告诉您,任何需要精确值的代码本质上都是“脆弱的”。这并不是说您可能无法获得精确的值,但这在很大程度上取决于输入数据。

我希望这有帮助。

In general, relying on the "exactness" of a floating-point value that is the result of a calculation is fraught with danger. So for example the following code:

CGFloat x1 = some_function();
CGFloat x2 = some_other_function();
if(x1 == x2)
{
    // do something
}

without even knowing what the functions do is most likely incorrect. Even if the functions do very similar calculations the optimizer may have reordered operations causing small rounding errors sufficient to cause the equality test to fail.

It should be:

CGFloat x1 = some_function();
CGFloat x2 = some_other_function();
CGFloat tolerance = 0.1; // or some tolerance suitable for the calculation.
if(fabsf(x1 - x2) < tolerance)
{
    // do something
}

where tolerance is some value suitable for the calculation being performed.

So, without knowing the internals of CAKeyframeAnimation, I can tell you that any code that expects exact values would be inherently "fragile". This is not to say that you won't get exact values, you might, but it will depend a lot on the input data.

I hope this helps.

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