使按钮从3秒后消失然后重新出现(swift3)

时间:2017-08-24 16:33:46

标签: ios button swift3 timer hidden

每次按下按钮,我都希望隐藏按钮3秒钟,然后在3秒钟后,我希望按钮不被隐藏。

@IBOutlet var save: UIButton!
@IBAction func button(_ sender: Any) {

}

3 个答案:

答案 0 :(得分:2)

您可以安排在主线程上执行闭包,延迟时间为3秒,取消隐藏按钮。

@IBOutlet var save: UIButton!
@IBAction func button(_ sender: Any) {
    save.isHidden = true
    DispatchQueue.main.asyncAfter(deadline: .now()+3, execute: {
        save.isHidden = false
    })
}

答案 1 :(得分:2)

您可以使用CGD:

@IBOutlet var save: UIButton!
@IBAction func button(_ sender: Any) {
    self.button.alpha = 0.0
    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
        self.button.alpha = 1.0
    }
}

或者您可以使用perform(_:with:afterDelay:)

@IBOutlet var save: UIButton!
@IBAction func button(_ sender: Any) {
    self.button.alpha = 0.0
    perform(#selector(showButton), with: nil, afterDelay: 3)
}

@objc func showButton() {
    self.button.alpha = 1.0
}

答案 2 :(得分:1)

实际上,如果你使用谷歌,你会发现很多例子。使用这样的东西:

var timer: Timer!
@IBOutlet var save: UIButton!
@IBAction func button(_ sender: Any) {
    save.isHidden = true
    timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(runTimedCode), userInfo: nil, repeats: false)
}

func runTimedCode() {
    save.isHidden = false
}