根据时间戳计算动画 strokeStart?

Calculating animation strokeStart based on timestamps?

我不是 100% 确定这是否适合 Whosebug 作为一个编程问题,因为它主要是一个数学逻辑问题,我很难解决,所以如果不是我的问题。

我正在用下面的一些代码制作一个圆圈的动画:

// Set the animation duration appropriately
    animation.duration = duration

    // Animate from 0 (no circle) to 1 (full circle)
    animation.fromValue = strokeStart
    animation.toValue = 1

    // Do a linear animation (i.e. the speed of the animation stays the same)
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)

    // Set the circleLayer's strokeEnd property to 1.0 now so that it's the
    // right value when the animation ends.
    progressCircle.strokeEnd = 1.0

所以使用的关键值是持续时间、起始值和始终为 1 的结束值。

圆的总 360 度总是代表 240 秒的时间(4 分钟)。

我在创建圆圈时设置了一个函数来插入持续时间和起始值的值,但我遇到的困难是如何适当地计算出这些值。

例如,当我在其中一个圆圈中绘图时,我正在从服务器获取时间戳。假设抓取的时间戳正好是 120 秒前。圆圈应该已经画完了 1/2,剩下的 180 度圆圈应该在总共 4 分钟的剩余 120 秒内画完。另一个例子是,如果时间戳是在 3 分 50 秒前完成的,则通过将 strokeStart 设置为某个高百分比,圆圈基本上会立即被填满,并在接下来的 10 秒内填满。

我知道如何获取时间戳,我知道如何获取时间戳距离当前时间多少秒,但我不知道如何计算出strokeStart。

对此有什么想法吗?

因为一个圆是240秒,你知道经过了多少秒,就简单地除以。

let strokeStart = Double(secondsElapsed) / 240.0