开发者

Are the key frames in a CAKeyframeAnimation always hit exactly?

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜