//倒數(shù)60秒
#define oneMinute 60
static int _countDown = oneMinute;
//創(chuàng)建timmer
@property (nonatomic, weak) NSTimer *timer;
- (NSTimer *)timer{
if (!_timer) {
//每秒更新一次button的值
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateButton) userInfo:nil repeats:YES];
}
return _timer;
}
- (void)updateButton{
//每秒都會減一,模以60剩下的就是實際的值
_countDown = (_countDown - 1 + oneMinute) % oneMinute;
if (_countDown == 0) {//到了0秒的時候
//setFireDate: //重新設置定時器開始運行的時間
self.timer.fireDate = [NSDate distantFuture];
//到了0獲取驗證碼按鈕可以重新點擊
self.buttonGetIDCode.enabled = YES;
}
NSString *title = [NSString stringWithFormat:@"獲取成功%d", _countDown];
self.buttonGetIDCode.titleLabel.text = title;
[self.buttonGetIDCode setTitle:title forState:UIControlStateDisabled];
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者