Commit 92150d26e34b46f1172e1c480e2b183e5d52f87a
1 parent
ee4b3175
Exists in
parentassistant
验证码添加倒计时
Showing
2 changed files
with
27 additions
and
0 deletions
 
Show diff stats
ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/caoyang.xcuserdatad/UserInterfaceState.xcuserstate
No preview for this file type
ParentAssistant/ParentAssistant/Classes/controllers/my/Controller/Login/RegisterViewController.swift
| @@ -101,6 +101,7 @@ class RegisterViewController: UITableViewController,UITextFieldDelegate { | @@ -101,6 +101,7 @@ class RegisterViewController: UITableViewController,UITextFieldDelegate { | ||
| 101 | @IBAction func getCodeAction(_ sender: UIButton) { | 101 | @IBAction func getCodeAction(_ sender: UIButton) { | 
| 102 | self.view.endEditing(true) | 102 | self.view.endEditing(true) | 
| 103 | if isMobilePhoneNumber(phoneTextField.text!) { | 103 | if isMobilePhoneNumber(phoneTextField.text!) { | 
| 104 | + setCodeButtonEnable(count: 60, btn: sender) | ||
| 104 | HTTPServer.shared.getCaptcha(phoneTextField.text!, completionHandler: { (str, error) -> Void in | 105 | HTTPServer.shared.getCaptcha(phoneTextField.text!, completionHandler: { (str, error) -> Void in | 
| 105 | httpJsonResule(jsonString: str, error: error, successHandler: { (json) -> Void in | 106 | httpJsonResule(jsonString: str, error: error, successHandler: { (json) -> Void in | 
| 106 | if !json["data"].boolValue{ | 107 | if !json["data"].boolValue{ | 
| @@ -125,6 +126,32 @@ class RegisterViewController: UITableViewController,UITextFieldDelegate { | @@ -125,6 +126,32 @@ class RegisterViewController: UITableViewController,UITextFieldDelegate { | ||
| 125 | passwordTextField.isSecureTextEntry = selected | 126 | passwordTextField.isSecureTextEntry = selected | 
| 126 | } | 127 | } | 
| 127 | 128 | ||
| 129 | + /// MARK - 设置验证码按钮读秒 | ||
| 130 | + func setCodeButtonEnable(count:Int,btn:UIButton){ | ||
| 131 | + btn.isEnabled = false | ||
| 132 | + var remainingCount : Int = count { | ||
| 133 | + willSet{ | ||
| 134 | + btn.setTitle("\(newValue)", for: UIControlState.normal) | ||
| 135 | + if newValue <= 0 { | ||
| 136 | + btn.setTitle("获取", for: UIControlState.normal) | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + let codeTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global()) | ||
| 141 | + codeTimer.schedule(deadline: .now(), repeating: .seconds(1)) | ||
| 142 | + codeTimer.setEventHandler { | ||
| 143 | + DispatchQueue.main.async { | ||
| 144 | + remainingCount-=1 | ||
| 145 | + if remainingCount<=0 { | ||
| 146 | + btn.isEnabled = true | ||
| 147 | + codeTimer.cancel() | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + codeTimer.resume() | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + | ||
| 128 | func isMobilePhoneNumber(_ mobile:String)->Bool { | 155 | func isMobilePhoneNumber(_ mobile:String)->Bool { | 
| 129 | let str="^1[0-9]{10}" | 156 | let str="^1[0-9]{10}" | 
| 130 | return NSPredicate(format: "SELF MATCHES %@", str).evaluate(with: mobile) | 157 | return NSPredicate(format: "SELF MATCHES %@", str).evaluate(with: mobile) |