定義兩個屬性//減少的秒數(shù)
@property(nonatomic,assign)NSIntegersecondsCountDown;//計時器
@property(nonatomic,strong)NSTimer*countDownTimer;
在發(fā)送驗證碼成功的時機(jī)里面
//設(shè)置不可點(diǎn)擊并開啟計時器
[selfsetRequestSmsCodeBtnCountDown];
//設(shè)置計時器并倒計時self.getButton是發(fā)送驗證碼那個button
-(void
)setRequestSmsCodeBtnCountDown{
[
self.getButtonsetEnabled:NO];//關(guān)閉交互
self.getButton.backgroundColor= [UIColorcolorWithRed:153.0/255green:153.0/255blue:153.0/255alpha:1.0];//灰色背景
self.secondsCountDown=60;//倒計時60秒
//設(shè)置并開啟計時器
self.countDownTimer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(countDownTimeWithSeconds:)userInfo:nilrepeats:YES
];
[
_countDownTimerfire
];
}
//計時器的執(zhí)行方法
-(
void)countDownTimeWithSeconds:(NSTimer
*)timerInfo{
//判斷當(dāng)前秒數(shù)如果秒數(shù)為0,重新設(shè)置button為可點(diǎn)擊狀態(tài)
if(_secondsCountDown==0
) {
[
self.getButtonsetEnabled:YES
];
self.getButton.backgroundColor= [UIColorredColor
];
[
self.getButtonsetTitle:@"獲取驗證碼"forState:UIControlStateNormal
];
[
_countDownTimerinvalidate
];
}
else
{
//如果不為0設(shè)置button字為當(dāng)前剩余秒數(shù),并繼續(xù)減少
[
self.getButtonsetTitle:[[NSNumbernumberWithLong:_secondsCountDown]description]forState:UIControlStateNormal
];
self.secondsCountDown
--;
}
}