1. viewDidload里配置倒计时Timer
self.seconds = 60;
self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod) userInfo:nil repeats:YES];
2. 每秒钟触发一次Timer
-(void)timerFireMethod {
if (self.seconds == 1) {
[self.countdownTimer invalidate];
self.countdownTimerLabel.text = @"";
self.seconds = 60;
[self.sendAgainButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
self.sendAgainButton.enabled = YES;
}else{
self.seconds--;
self.countdownTimerLabel.text = [NSString stringWithFormat:@"(%lu)",(unsigned long)self.seconds];
[self.sendAgainButton setTitleColor:[UIColor colorWithRed:189 / 255.f green:189 / 255.f blue:189 / 255.f alpha:1.0] forState:UIControlStateNormal];
self.sendAgainButton.enabled = NO;
}
}
3. Timer的release方法
- (void)releaseTimer {
if (self.countdownTimer) {
if ([self.countdownTimer respondsToSelector:@selector(isValid)]) {
if ([self.countdownTimer isValid]) {
[self.countdownTimer invalidate];
self.seconds = 60;
}
}
}
}