mapanguan

导航

Swift3 倒计时按钮扩展

extension UIButton{
    func overrideSelf(){
        self.setTitle("验证码", for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        self.addTarget(self, action: #selector(tf), for: .touchUpInside)
        self.setTitleColor(UIColor.orange, for: .normal)
        self.layer.cornerRadius = 15
        self.layer.borderWidth = 1
        self.layer.borderColor = color_navView.cgColor
        self.layer.masksToBounds = true
    }
    
    func tf(){
        Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction(timer:)), userInfo: nil, repeats: true).fire()
    }
    
    func timerAction(timer:Timer){
        tag -= 1
        if tag < 0 {
            self.isUserInteractionEnabled  = true
            self.layer.borderColor = color_navView.cgColor
            self.setTitle("重新获取", for: .normal)
            self.setTitleColor(UIColor.orange, for: .normal)
            timer.invalidate()
            tag = 30
        }else{
            self.isUserInteractionEnabled = false
            self.layer.borderColor = UIColor.gray.cgColor
            self.setTitle("剩余\(tag)s", for: .normal)
            self.setTitleColor(UIColor.gray, for: .normal)
        }
    }
    
    func getVerificationCode(){
       //let st = STools()
       // st.getVercodeAction(cityCode: appDelegate.cityCode, mobile: appDelegate.vphone, verificationCodeType: appDelegate.verificationStr)
       // st.verificationInfo { (info:String) in
       //    if info != "验证码已发送"{//失败了
       //       self.tag = 0
       //   }
       //     appDelegate.window.rootViewController?.showHint(info)
       // }
  }
}

//MARK:调用
        codeBtn.overrideSelf()
        codeBtn.tag = 30
        codeBtn.addTarget(self, action: #selector(getVercodeAction(_:)), for: .touchUpInside)

    func getVercodeAction(_ sender: Any) {
        codeBtn.getVerificationCode()
    }

  

posted on 2017-02-10 15:53  mapanguan  阅读(211)  评论(0)    收藏  举报