开始计时调用下面这个方法就可以了。获取验证码的按钮就会开始显示倒计时直到60s后恢复最初状态
[self startTime];
实现代码如下:
定义定时器和时间
@implementation LoginViewController {
NSTimer *_timer;
int _waiTime;
}
#pragma mark--开始计时
- (void)startTime {
[_timer invalidate];
_waiTime = 60;
[self updateVareyButton];
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateWaitTime)
userInfo:nil
repeats:YES];
}
- (void)updateVareyButton {
NSString *title = @"获取验证码";
if (_waiTime > 0) {
title = [NSString stringWithFormat:@"%d秒后重发", _waiTime];
[self.getVerfyCodeBtn setTitleColor:SecondColor forState:UIControlStateNormal];
self.getVerfyCodeBtn.userInteractionEnabled = NO;
}
[self.getVerfyCodeBtn setTitle:title forState:UIControlStateNormal];
}
- (void)updateWaitTime {
_waiTime = _waiTime - 1;
[self updateVareyButton];
if (_waiTime <= 0) {
[self.getVerfyCodeBtn setTitleColor:MainColor forState:UIControlStateNormal];
self.getVerfyCodeBtn.userInteractionEnabled = YES;
[_timer invalidate];
_timer = nil;
}
}
网友评论