美文网首页
swift3.0-用GCD实现验证码倒计时效果

swift3.0-用GCD实现验证码倒计时效果

作者: 唉哦诶斯 | 来源:发表于2017-07-12 16:44 被阅读34次

    //获取验证码 @IBAction func getVerificationCode(_ sender: UIButton) { //先让按钮不能点 sender.isUserInteractionEnabled = false var time = 60 let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global()) //开始时间,时间间隔 timer.scheduleRepeating(wallDeadline: .now(), interval: 1) //每个时间段执行的方法 timer.setEventHandler { time -= 1 //回到主线程更新按钮标题 DispatchQueue.main.sync(execute: { sender.setTitle("\(time)s", for: .normal) }) //倒计时到0时,设置默认标题,打开用户交互,取消定时任务 if time == 0 { DispatchQueue.main.sync(execute: { sender.setTitle("验证", for: .normal) sender.isUserInteractionEnabled = true }) timer.cancel() } } //开启定时器 timer.resume() }

    相关文章

      网友评论

          本文标题:swift3.0-用GCD实现验证码倒计时效果

          本文链接:https://www.haomeiwen.com/subject/gxibhxtx.html