@property (nonatomic ,assign)NSInteger secondsCountDown;//倒计时的总时间
@property (nonatomic ,strong)NSTimer *countDownTimer;//定时器
//写入BUT点击事件
-(void)buttonDidClock{
//设置倒计时总时长
self.secondsCountDown = 60;
self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimeNSTimer) userInfo:nil repeats:YES];
[self.capthaButton setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.secondsCountDown] forState:UIControlStateNormal];
}
//使用NSTimer实现倒计时
- (void)startTimeNSTimer
{
self.secondsCountDown -- ;
self.capthaButton.userInteractionEnabled = NO;
[self.capthaButton setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.secondsCountDown] forState:UIControlStateNormal];
if (self.secondsCountDown == 0) {
[self.countDownTimer invalidate];
self.capthaButton.userInteractionEnabled = YES;
[self.capthaButton setTitle:@"重新发送验证码" forState:UIControlStateNormal];
}
}
网友评论