🌤️ 加载中...
#
IOS 2019-03-27

IOS开发笔记-点击按钮倒计时

By Jove 1 Views 4 MIN READ 0 Comments

//前提是新建一个按钮  @IBOutlet weak var edit: UIButton!

func timeChange() {
        var time = 100 //从100倒计时
        let codeTimer = DispatchSource.makeTimerSource(flags: .init(rawValue: 0), queue: DispatchQueue.global())
        codeTimer.schedule(deadline: .now(), repeating: .milliseconds(1000))
        codeTimer.setEventHandler {
            time = time - 1
            DispatchQueue.main.async {
             self.edit.isEnabled = false//点击按钮后,禁用按钮显示倒计时
            }
            if time < 0 {
                codeTimer.cancel()
                DispatchQueue.main.async {
                    self.edit.isEnabled = true
                    self.edit.setTitle("测试", for: .normal)
                }
                return
            }
            DispatchQueue.main.async {
                self.edit.setTitle("\(time)", for: .normal)
            }
        }
        codeTimer.activate()
    }
//调用时直接在同样的按钮里带入函数就可以
@IBAction func edit(_ sender: Any) {
timeChange()
}

本文由 Jove 原创

采用 CC BY-NC-SA 4.0 协议进行许可

转载请注明出处:https://www.jozxing.cc/index.php/archives/1580/

TAGS: 无标签

相关推荐

  • 暂无相关推荐,看看别的吧。

0 评论