是否可以控制 CAAnimation 的时间线?

发布于 2024-12-11 21:26:33 字数 189 浏览 0 评论 0原文

我有一个复杂的动画,通常由某个 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 技术交流群。

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

发布评论

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

评论(1

毅然前行 2024-12-18 21:26:33

这是可能的(而且很简单),但我只是尝试将此作为一个实验(对于由捏合手势识别器驱动的复杂动画),所以我很想听听这个解决方案是否足够:

您需要设置您的速度将动画设置为 0 以及到要跳转到的时间点的时间偏移,例如,

CABasicAnimation* animation = [CABasicAnimation ...];
animation.speed = 0;
animation.duration = 1;
animation.timeOffset = 0.5;

将使动画跳转到半秒后的点。

现在,您无法在将 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.

CABasicAnimation* animation = [CABasicAnimation ...];
animation.speed = 0;
animation.duration = 1;
animation.timeOffset = 0.5;

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 ;).

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