属性
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (nonatomic, assign) NSInteger secondsCountDown;
@property (nonatomic, strong) NSTimer *countDownTimer;
方法
//倒计时
- (void)timeOutAction{
_secondsCountDown = 60;
[_getCodeBtn setTitle:@"60 s" forState:(UIControlStateNormal)];
_getCodeBtn.backgroundColor = kSubTextColor;
_getCodeBtn.enabled = NO;
self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}
-(void)timeFireMethod{
[_getCodeBtn setTitle:[NSString stringWithFormat:@"%ld s",(long)_secondsCountDown] forState:(UIControlStateNormal)];
_secondsCountDown--;
NSLog(@"%ld",_secondsCountDown);
if(_secondsCountDown == -1){
[_getCodeBtn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
_getCodeBtn.backgroundColor = kBlueColor;
_getCodeBtn.enabled = YES;
[_countDownTimer invalidate];
}
}