【问题标题】:Preserve animating UIView property after removing animations, but without using a class variable?删除动画后保留动画 UIView 属性,但不使用类变量?
【发布时间】:2016-01-14 16:37:27
【问题描述】:

删除动画后保留动画 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()
            }
    })
}

【问题讨论】:

    标签: ios swift uiview uikit uiviewanimation


    【解决方案1】:

    我可能会亲自使用变量。如果你真的讨厌这样,我可能会暂停动画,而不是删除它。

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

    【讨论】:

    • 仅仅暂停动画有什么缺点吗?这看起来干净多了!
    • 还有为什么需要timeOffset
    • 我能想到的唯一缺点是您将在内存中浮动动画。我怀疑会有太多的开销。您可能不需要 timeOffset - 如果您永远不会再次恢复动画,则不需要跟踪偏移量。如果您要恢复,那么您将需要跟踪您暂停了多长时间(currentMediaTime-timeOffset)并将其设置为您的新开始时间。
    • 嗯,用rogressBar.layer.speed = 0 替换progressBar.layer.removeAllAnimations() 会导致进度条消失。有什么建议吗?
    • developer.apple.com/library/ios/documentation/Cocoa/Conceptual/… 有一些显示暂停和恢复的代码。也许毕竟需要偏移量?
    【解决方案2】:

    这就是最终奏效的方法。关键是阅读progressBar.layer.presentationLayer()!.frame.width

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

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 2010-12-13
      • 2018-07-02
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多