iOS 驗證碼倒計時

屬性

@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (nonatomic, assign) NSInteger secondsCountDown;
@property (nonatomic, strong) NSTimer *countDownTimer;

方法

//倒計時
- (void)timeOutAction{
    _secondsCountDown = 60;
    [_getCodeBtn setTitle:@"60 s" forState:(UIControlStateNormal)];
    _getCodeBtn.backgroundColor = kSubTextColor;
    _getCodeBtn.enabled = NO;
    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}

-(void)timeFireMethod{
    [_getCodeBtn setTitle:[NSString stringWithFormat:@"%ld s",(long)_secondsCountDown] forState:(UIControlStateNormal)];
    _secondsCountDown--;
    NSLog(@"%ld",_secondsCountDown);
    if(_secondsCountDown == -1){
        [_getCodeBtn setTitle:@"獲取驗證碼" forState:(UIControlStateNormal)];
        _getCodeBtn.backgroundColor = kBlueColor;
        _getCodeBtn.enabled = YES;
        [_countDownTimer invalidate];
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章