swift gcd 定时器。获取验证码

 //获取验证码

    func getCodeButtonTouch(sender:UIButton) {

        //在获取期间不允许点击

        sender.backgroundColor = UIColor.gray

        sender.isUserInteractionEnabled = false

        

        var count = 60;

        let timer = DispatchSource.makeTimerSource()


        timer.setEventHandler {

            count -= 1

            DispatchQueue.main.async {

                self.codeBtn?.setTitle("获取(\(count)s)", for: UIControlState.normal)

            }

        }

        timer.setCancelHandler {

            DispatchQueue.main.async {

                self.codeBtn?.isUserInteractionEnabled = true

                self.codeBtn?.backgroundColor = MAIN_COLOR

                self.codeBtn?.setTitle("获取(60s)", for: UIControlState.normal)

            }

        }

        //间隔总时间 60

        timer.scheduleRepeating(deadline: .now(), interval: .seconds(1), leeway: .milliseconds(60))

        timer.resume()

        //59秒时执行

        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(59), execute:{

            timer.cancel()

        })

        //获取验证码

        getCodeRequest()

        //收起 键盘

        self.view.endEditing(true)

        

    }


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值