設置timer,用於otp

#pragma mark - Countdown algorithm

- (void)setUpTimer {

    self.timeLeft = 15;

//設置resend按鈕爲隱藏,倒計時label爲顯示,這種倒計時最好是不要用button做顯示,不好看,效果也不明顯,具體我給忘了。因爲本來是想直接就用button的,就是title顯示不同,結果證明不行

    [self.resendVerifyCodeButton setHidden:YES];

    [self.sendEmailCountTimeLabel setHidden:NO];

    [self.sendEmailCountTimeLabel setAlpha:0.5f];//保證半失焦的狀態,灰暗

    [self reloadCountdownLabel];

    [self.countdownTimer invalidate];

    self.countdownTimer = nil;

    self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];

}

 

- (void)reloadCountdownLabel {

//倒計時過程中顯示的文本

    if (self.timeLeft >= 0) {

        [self.sendEmailCountTimeLabel setText:[NSString stringWithFormat:@"Resend verification email\n (After %i seconds)",self.timeLeft]];

    }

    else {

        [self.sendEmailCountTimeLabel setText:[NSString stringWithFormat:@"Resend verification email\n (After %i seconds)",0]];

    }

}

- (void)countDown:(NSTimer *)timer {

    self.timeLeft--;

    [self reloadCountdownLabel];

    if (self.timeLeft == -1) {

        [self.countdownTimer invalidate];

        self.countdownTimer = nil;

//倒計時結束的UI:

        [self.resendVerifyCodeButton setHidden:NO];

        [self.sendEmailCountTimeLabel setHidden:YES];

    }

}


使用方式比較簡單啦:

點擊完resend button,call SMS api成功之後,就可以開始倒計時了。這裏就不多寫了

- (IBAction)resendVerifyCodeAction:(id)sender {

            [self setUpTimer];

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章