From 92150d26e34b46f1172e1c480e2b183e5d52f87a Mon Sep 17 00:00:00 2001 From: Cao yang Date: Mon, 21 May 2018 10:55:21 +0800 Subject: [PATCH] 验证码添加倒计时 --- ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/caoyang.xcuserdatad/UserInterfaceState.xcuserstate | Bin 416996 -> 0 bytes ParentAssistant/ParentAssistant/Classes/controllers/my/Controller/Login/RegisterViewController.swift | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 0 deletions(-) diff --git a/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/caoyang.xcuserdatad/UserInterfaceState.xcuserstate b/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/caoyang.xcuserdatad/UserInterfaceState.xcuserstate index a93191d..30a48ac 100644 Binary files a/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/caoyang.xcuserdatad/UserInterfaceState.xcuserstate and b/ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/caoyang.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ParentAssistant/ParentAssistant/Classes/controllers/my/Controller/Login/RegisterViewController.swift b/ParentAssistant/ParentAssistant/Classes/controllers/my/Controller/Login/RegisterViewController.swift index 240a2e3..9c41df9 100644 --- a/ParentAssistant/ParentAssistant/Classes/controllers/my/Controller/Login/RegisterViewController.swift +++ b/ParentAssistant/ParentAssistant/Classes/controllers/my/Controller/Login/RegisterViewController.swift @@ -101,6 +101,7 @@ class RegisterViewController: UITableViewController,UITextFieldDelegate { @IBAction func getCodeAction(_ sender: UIButton) { self.view.endEditing(true) if isMobilePhoneNumber(phoneTextField.text!) { + setCodeButtonEnable(count: 60, btn: sender) HTTPServer.shared.getCaptcha(phoneTextField.text!, completionHandler: { (str, error) -> Void in httpJsonResule(jsonString: str, error: error, successHandler: { (json) -> Void in if !json["data"].boolValue{ @@ -125,6 +126,32 @@ class RegisterViewController: UITableViewController,UITextFieldDelegate { passwordTextField.isSecureTextEntry = selected } + /// MARK - 设置验证码按钮读秒 + func setCodeButtonEnable(count:Int,btn:UIButton){ + btn.isEnabled = false + var remainingCount : Int = count { + willSet{ + btn.setTitle("\(newValue)", for: UIControlState.normal) + if newValue <= 0 { + btn.setTitle("获取", for: UIControlState.normal) + } + } + } + let codeTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global()) + codeTimer.schedule(deadline: .now(), repeating: .seconds(1)) + codeTimer.setEventHandler { + DispatchQueue.main.async { + remainingCount-=1 + if remainingCount<=0 { + btn.isEnabled = true + codeTimer.cancel() + } + } + } + codeTimer.resume() + } + + func isMobilePhoneNumber(_ mobile:String)->Bool { let str="^1[0-9]{10}" return NSPredicate(format: "SELF MATCHES %@", str).evaluate(with: mobile) -- libgit2 0.21.0