定义两个属性//减少的秒数
@property(nonatomic,assign)NSIntegersecondsCountDown;//计时器
@property(nonatomic,strong)NSTimer*countDownTimer;
在发送验证码成功的时机里面
//设置不可点击并开启计时器
[selfsetRequestSmsCodeBtnCountDown];
//设置计时器并倒计时self.getButton是发送验证码那个button
-(void
)setRequestSmsCodeBtnCountDown{
[
self.getButtonsetEnabled:NO];//关闭交互
self.getButton.backgroundColor= [UIColorcolorWithRed:153.0/255green:153.0/255blue:153.0/255alpha:1.0];//灰色背景
self.secondsCountDown=60;//倒计时60秒
//设置并开启计时器
self.countDownTimer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(countDownTimeWithSeconds:)userInfo:nilrepeats:YES
];
[
_countDownTimerfire
];
}
//计时器的执行方法
-(
void)countDownTimeWithSeconds:(NSTimer
*)timerInfo{
//判断当前秒数如果秒数为0,重新设置button为可点击状态
if(_secondsCountDown==0
) {
[
self.getButtonsetEnabled:YES
];
self.getButton.backgroundColor= [UIColorredColor
];
[
self.getButtonsetTitle:@"获取验证码"forState:UIControlStateNormal
];
[
_countDownTimerinvalidate
];
}
else
{
//如果不为0设置button字为当前剩余秒数,并继续减少
[
self.getButtonsetTitle:[[NSNumbernumberWithLong:_secondsCountDown]description]forState:UIControlStateNormal
];
self.secondsCountDown
--;
}
}
网友评论