Ios 使用负速度恢复驾驶?

Ios 使用负速度恢复驾驶?,ios,objective-c,xcode,uiview,calayer,Ios,Objective C,Xcode,Uiview,Calayer,我用以下代码恢复我的CABASICANIATION: CFTimeInterval pausedtime = layer.timeOffset; layer.speed = 1.0; layer.timeOffset = 0.0; layer.beginTime = [layer convertTime:CACurrentMediaTime() toLayer:nil] - pausedTime; 这很简单。在文章的最后,作者指出,如果速度为负值,动画就会反转。

我用以下代码恢复我的CABASICANIATION:

    CFTimeInterval pausedtime = layer.timeOffset;
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = [layer convertTime:CACurrentMediaTime() toLayer:nil] - pausedTime;
这很简单。在文章的最后,作者指出,如果速度为负值,动画就会反转。我不明白在这种情况下,
timeOffset
beginTime
应该是什么样子


p.S.我知道我可以通过从表示层获取当前值并设置为Value和fromValue来反转动画。我想知道速度属性是否可行。

使用
向后恢复动画时会遇到问题。timeOffset=0
.timeOffset=0
表示动画处于其开始。因此,使用反向速度,动画将立即完成。因此,例如,如果你有一个动画,它在完成时会被自动删除——它不会播放任何东西:只是会消失

CAAnimation的文档确实很差。他们的恢复示例仅使用
.beginTime
属性。我们可以改用
.timeOffset
属性

1。将图层的开始时间设置为其容器的当前时间:

// set begin time to current time of superlayer to start immediately
let sTime = layer.superlayer?.convertTime(CACurrentMediaTime(), to: nil) ?? 0
layer.beginTime = sTime
// set starting point of animation to the current "progress"
layer.timeOffset = progress
// set negative speed to have reverse animation
layer.speed = -1.0
2。使用
.timeOffset

// set begin time to current time of superlayer to start immediately
let sTime = layer.superlayer?.convertTime(CACurrentMediaTime(), to: nil) ?? 0
layer.beginTime = sTime
// set starting point of animation to the current "progress"
layer.timeOffset = progress
// set negative speed to have reverse animation
layer.speed = -1.0
其中,
progress
是要从动画持续时间方面恢复的时间位置。i、 e.如果动画持续时间为0.6-则
0≤ 进展≤ 0.6

3。反向启动动画:

// set begin time to current time of superlayer to start immediately
let sTime = layer.superlayer?.convertTime(CACurrentMediaTime(), to: nil) ?? 0
layer.beginTime = sTime
// set starting point of animation to the current "progress"
layer.timeOffset = progress
// set negative speed to have reverse animation
layer.speed = -1.0
如果需要,不要忘记在动画完成时删除动画

下面是这些设置实际上是如何应用的:


这张照片的来源是什么?我自己画的:)@DmytroBabych我发现你的答案对理解引擎盖下运行的基本动力非常有帮助,非常感谢,我给你留下了一张赞成票。不过,当我尝试使用你建议的方法反转动画时,它还是超级灵巧。你能帮我看看我的问题吗?我将非常感激!我制作了一个Xcode游戏场,以交互方式解释和探索核心动画。嘿,你最终能做到这一点吗?@oneshot不幸没有成功(