获取验证码倒计时


title : 获取验证码倒计时
category : 常用封装


获取验证码倒计时

标签(空格分隔): 常用封装



#import "requestSmsViewController.h"

@interface requestSmsViewController ()

/** 获取验证码按钮 */
@property (weak, nonatomic) IBOutlet UIButton *requestSmsCodeBtn;
/** 倒计时 */
@property (nonatomic, assign) unsigned secondsCountDown;
/** 定时器 */
@property (nonatomic, weak) NSTimer *countDownTimer;

@end

@implementation requestSmsViewController

/**
 *  获取验证码
 */
- (IBAction)requestSmsCodeBtnClick {
    
    // 触发倒计时
    [self setRequestSmsCodeBtnCountDown];
}

/**
 *  点击获取验证码后进入倒计时
 */
- (void)setRequestSmsCodeBtnCountDown
{
    [self.requestSmsCodeBtn setEnabled:NO];
    self.secondsCountDown = 60;
    
    self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownTimeWithSeconds:) userInfo:nil repeats:YES];
    [self.countDownTimer fire];
}

/**
 *  倒计时状态
 */
- (void)countDownTimeWithSeconds:(NSTimer*)timerInfo
{
    if (self.secondsCountDown == 0) {
        [self.requestSmsCodeBtn setEnabled:YES];
        [self.requestSmsCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.countDownTimer invalidate];
    } else {
        [self.requestSmsCodeBtn setTitle:[NSString stringWithFormat:@"剩余%@s",[[NSNumber numberWithInt:self.secondsCountDown] description]] forState:UIControlStateNormal];
        self.secondsCountDown--;
    }
}

@end

你可能感兴趣的:(获取验证码倒计时)