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



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

具体来说,目标是对 10 秒的进度条进行动画处理。

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

到目前为止,唯一的解决方案是使用类变量,但它似乎很笨拙,少一个要跟踪的变量将是理想的。没有类变量这可能吗?

我们尝试使用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
    }

最新更新