1.首先创建一个获取验证码的按钮 按钮绑定方法 然后实现getAuthCodeBtnClick的方法
// 初始化获取验证码按钮
self.getAuthCodeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 24 - 135, 57, 135, 36)];
[self.getAuthCodeBtn addTarget:self action:@selector(getAuthCodeBtnClick) forControlEvents:UIControlEventTouchUpInside];
self.getAuthCodeBtn.layer.masksToBounds = YES;
self.getAuthCodeBtn.titleLabel.font = FontNeveLightWithSize(12.0);
self.getAuthCodeBtn.layer.borderWidth = 1;
[self.getAuthCodeBtn setTitleColor:AppBlueColor forState:UIControlStateNormal];
[self.getAuthCodeBtn setTitleColor:HintTextColor forState:UIControlStateDisabled];
self.getAuthCodeBtn.backgroundColor = [UIColor whiteColor];
[whiteView addSubview:self.getAuthCodeBtn];
[self updateButtonState:ButtonStateClickable];
2.实现getAuthCodeBtn的点击方法
/ 点击获取验证码按钮
- (void)getAuthCodeBtnClick {
NSLog(@"%@", NSStringFromSelector(_cmd));
NSString *phoneNumber = [self.phoneNumberTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
// 验证手机号码
if (phoneNumber.length == 0) {
[self showHUDErrorWithText:@"Please enter your phone number"];
} else {
[self.authCodeTextField becomeFirstResponder];
// 开始倒数计时
[self startWithSeconds:60];
[self updateButtonState:ButtonStateDisable];
NSString *countryCode = self.countryCodeLabel.text;
phoneNumber = [NSString stringWithFormat:@"%@ %@", countryCode, phoneNumber];
[HttpManager getAuthCodeByPhoneNumber:phoneNumber chc_type:@"1" success:^(AFHTTPRequestOperation *operation, id responseObject) {
userRegisterInfo = (NSDictionary *)responseObject[@"result"];
NSInteger rtnCode = [responseObject[@"code"] integerValue];
if (rtnCode == 0) {
NSString *msg = [NSString stringWithFormat:@"Verification code sent to your %@ Phone Number", phoneNumber];
[self showHUDDoneWithText:msg];
// self.authCodeTextField.text = userRegisterInfo[@"ci_password"];
} else if (rtnCode == 1) {// 已经注册过了
[self resetTimer];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"This number is already registered to a NiHao account. Do you want to log in to NiHao now?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alertView show];
} else {
[self resetTimer];
[self processServerErrorWithCode:rtnCode andErrorMsg:[responseObject objectForKey:@"message"]];
}
} failBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
[self showHUDNetError];
[self stop];
}];
}
}
3.启动定时器
/ 开始倒数计时
-(void)startWithSeconds:(int)totalSecond {
_totalSeconds = totalSecond;
_second = totalSecond;
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
_startDate = [NSDate date];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
4定时器的执行方法
-(void)timerStart:(NSTimer *)theTimer {
double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
_second = _totalSeconds - (int)(deltaTime + 0.5) ;
if (_second < 0.0) {
[self stop];
} else {
NSString *title = [NSString stringWithFormat:@"%d seconds", _second];
[self.getAuthCodeBtn setTitle:title forState:UIControlStateNormal];
[self.getAuthCodeBtn setTitle:title forState:UIControlStateDisabled];
}
}
5.定时器的关闭和重启代码
// 停止倒数计时
- (void)stop {
[self stopTimerWithText:@"Resend Code"];
}
// 重置倒数计时
- (void)resetTimer {
[self stopTimerWithText:@"Get verification code"];
}
- (void)stopTimerWithText:(NSString *)text {
if (_timer) {
if ([_timer respondsToSelector:@selector(isValid)]) {
if ([_timer isValid]) {
[_timer invalidate];
_second = _totalSeconds;
[self.getAuthCodeBtn setTitle:text forState:UIControlStateNormal];
[self.getAuthCodeBtn setTitle:text forState:UIControlStateDisabled];
[self updateButtonState:ButtonStateClickable];
}
}
}
}