在删除动画后保留动画 UIView 属性,但不使用 class 变量?

Preserve animating UIView property after removing animations, but without using a class variable?

移除动画后保留动画 UIView 属性 的正确方法是什么?

具体来说,目标是制作 10 秒进度条的动画。

如果用户在 5 秒后停止,进度条应保持在 5 秒。相反,一旦动画被移除,它就会跳到 10 秒,即最终值。

到目前为止,唯一的解决方案是使用 class 变量,但这似乎很老套,少一个变量来跟踪是理想的。如果没有 class 变量,这可能吗?

我们尝试使用 presentationLayer 中的值,如下所示,但也失败了。

private func animateProgressBar(recording: Bool) {
    // Stop animation if not recording
    if (!recording) {
        print("Stopped recording")
        progressBar.layer.removeAllAnimations()
        return
    }

    // If here, animate progress bar
    view.layoutIfNeeded()
    UIView.animateWithDuration(VideoDur, delay: 0.0, options: .CurveLinear, animations: {
        self.progressBarWidthConstraint.constant = self.view.frame.width
        self.view.layoutIfNeeded()
        }, completion: { finished in
            if (finished) {
                self.endRecording()
            } else {
                self.progressBarWidthConstraint.constant = self.progressBar.layer.presentationLayer()!.frame.width
                self.view.layoutIfNeeded()
            }
    })
}

我个人可能会选择变量。如果你真的讨厌那个,我可能只是暂停动画,而不是删除它。

progressBar.layer.speed = 0;
progressBar.layer.timeOffset = mediaTimeForLayer;

这是最终起作用的方法。关键是阅读 progressBar.layer.presentationLayer()!.frame.width.

    if (!recording) {
        print("Stopped progress bar")
        progressBarWidthConstraint.constant = progressBar.layer.presentationLayer()!.frame.width
        progressBar.layer.removeAllAnimations()
        view.layoutIfNeeded()
        return
    }