是否可以控制 CAAnimation 的时间线?
我有一个复杂的动画,通常由某个 CAMediaTimingFunction 驱动,自行运行。这很好用。
现在,我想使用外部值(例如来自滑块或手势识别器)来控制同一动画的时间(线)。换句话说,我不想让“时钟”驱动时间线,而是让滑块来驱动,这样就可以用它来回擦洗,并通过将滑块设置为某个值来“冻结”动画。
这可能吗?如果是这样,怎么办?
I have a complex animation that usually runs just by itself, driven by a certain CAMediaTimingFunction
. This works fine.
Now, I want to control that same animation's time(line) using an external value, for example from a slider or a gesture recognizer. In other words, I don't want to have the "clock" drive the timeline, but a slider, so one can scrub back and forth with it and "freeze" the animation by putting the slider to a certain value.
Is this possible? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可能的(而且很简单),但我只是尝试将此作为一个实验(对于由捏合手势识别器驱动的复杂动画),所以我很想听听这个解决方案是否足够:
您需要设置您的速度将动画设置为 0 以及到要跳转到的时间点的时间偏移,例如,
将使动画跳转到半秒后的点。
现在,您无法在将 CAAnimation 对象添加到图层后对其进行操作,因此每次偏移量发生变化时您都需要添加一个新的动画(并删除旧的动画,不要忘记;)。
It's possible (and quite easy), but I only tried this as an experiment (for a complex animation driven by a pinch gesture recognizer), so I'd love to hear if this solution is sufficient:
You need to set the speed of your animation to 0 and the time offset to the point in time you want to jump to, e.g.
will make the animation jump to the point it would be after half a second.
Now, you can't manipulate CAAnimation objects after they've been added to the layer, so you'll need to add a new animation every time the offset changes (and remove the old one, don't forget ;).