macos - 太阳经过天空仅需10小时

标签 macos cocoa animation core-animation osx-lion

我正在编写一个玩具应用程序来试验一些核心动画功能,包括沿着路径(这就是太阳运动的位置)制作动画和操纵时间。

https://github.com/boredzo/WatchCompass

Animated screenshot of the app, showing the sun and the clock's hour hand going around.

(别介意按钮,它还没有实现。)

太阳和表盘是 CALayers,每个都包含一个静态图像。时针是表盘层内的 CAShapeLayer,其 anchor 设置为一端 ((NSPoint){ 0.5, 1.0 })。

使用 CAKeyframeAnimation 沿路径对太阳进行动画处理。椭圆表示路径;您可以看到它们由于某种原因没有排队,但这是另一个问题。

时针的 transform.rotation.z 使用 CABasicAnimation 进行动画处理,如 described in this answer .

问题(至少是我在这个问题中询问的问题)是持续时间的差异。

两个动画都设置为完全相同的持续时间,但太阳比时针早整整两个时钟小时回到起始位置。

当然,最终时钟的持续时间将恰好是太阳持续时间的一半(或其速度设置为 2),因为时钟只有 12 小时。如果我这样做,那么时针就会落后太阳 4 个时钟小时,而不是 2 个时钟小时。

Screenshot of the clock going around twice per virtual day and arriving four clock-hours late.

那么,鉴于两个动画具有相同的持续时间,或者时钟动画的持续时间是太阳动画的偶数倍,为什么时钟需要更长的时间?

就此而言,虽然我没有提示,但为什么太阳要等待时钟 catch 呢?

最佳答案

这似乎是由于您没有为关键帧动画的 keyTimes 属性指定值。根据文档:

For the best results, the number of elements in the array should match the number of elements in the values property or the number of control points in the path property. If they do not, the timing of your animation might not be what you expect.

事实上,将 keyTimes 设置为 @[ @0, @0.25, @0.5, @0.75, @1 ] 似乎可以纠正此问题。

关于macos - 太阳经过天空仅需10小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519050/

相关文章:

ios - 回到前台时重新启动动画

macos - CFRunLoopObserver 与嵌套 CFRunLoops

macos - 在 OS X (10.9.5) 上加载 gnuplot 脚本时出现问题

macos - 在 mac 10.5.8 上运行 tomcat 7.0.16 时出现问题

objective-c - 以编程方式禁用列的 "create sort descriptor"

objective-c - 为什么 'no known method for selector x'在ARC下是硬错误?

swift - 如何在使用我的 Mac 应用程序的查找器中添加上下文菜单

c - 尝试在 C99 模式下编译 C 程序代码时出错

JavaScript 翻转计数器

java - 离开 parent 时Android View 消失