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

来源:互联网 发布:asp域名授权码程序dll 编辑:程序博客网 时间:2024/05/22 17:02

1. viewDidload里配置倒计时Timer

//set the countdown timerself.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;            }        }    }}
0 0
原创粉丝点击