【问题标题】:How to prevent a UIView.animate from being called如何防止 UIView.animate 被调用
【发布时间】:2018-10-09 09:09:53
【问题描述】:

我想重现 WhatsApp 按钮在正在进行的通话视图上的行为:它们在出现几秒钟后消失,而每次用户点击屏幕时,它们都会再次出现。

假设我有这两个按钮,

@IBOutlet weak var callButton: UIButton!
@IBOutlet weak var muteButton: UIButton!

这是输入viewDidAppear以及用户点击屏幕时调用的sn-p:

self.callButton.alpha = 1.0
self.muteButton.alpha = 1.0
delay(4.0) {
    UIView.animate(withDuration: 1.0, animations: {
        self.callButton.alpha = 0.0
        self.muteButton.alpha = 0.0
    }, completion: { _ in })
}

func delay(_ seconds: Double, completion: @escaping () -> ()) {
    let popTime = DispatchTime.now() + Double(Int64(Double(NSEC_PER_SEC) * seconds)) / Double(NSEC_PER_SEC)
    DispatchQueue.main.asyncAfter(deadline: popTime) {
        completion()
    }
}

使用此代码,如果用户在上次通话后 3 秒点击屏幕,按钮仍会在 1 秒后消失。所以我想知道如果同时再次点击视图,我如何阻止以前的UIView.animate

感谢您的帮助

【问题讨论】:

    标签: ios swift uiview uiviewanimation


    【解决方案1】:

    首先,当 Apple 在UIView.animate 中向您提供了延迟方法时,您为什么要创建延迟方法?

    现在,要实现你想要的,只需使用一个标志来检查该方法是否已经被调用过一次并阻止该方法调用。

    var animating = false
    
    func yourAnimateMethod() {
        if !animating {
            animating = true
            UIView.animate(withDuration: 1, delay: 4, options: .curveLinear, animations: {
                self.callButton.alpha = 0.0
                self.muteButton.alpha = 0.0
            }) { (completed) in
                if completed {
                    animating = false
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 2015-04-23
      • 2017-08-10
      • 2021-12-27
      • 1970-01-01
      • 2017-06-07
      相关资源
      最近更新 更多