您的位置:首页 > 移动开发 > IOS开发

iOS发送验证码倒计时功能的实现

2015-06-13 20:46 791 查看

1. viewDidload里配置倒计时Timer

//set the countdown 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;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: