NSTimer 验证码倒计时

2016-08-18  本文已影响73人  走道牙的人

参考http://blog.csdn.net/samuelltk/article/details/7484533

参考http://www.cnblogs.com/pigpigDD/p/3975852.html

参考http://blog.csdn.net/openglnewbee/article/details/51508301

验证码倒计时示例代码

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.oldGetVerifyBtn.tag = 59;
}

/**
 *  获取原手机号验证码
 */
- (IBAction)getOldVerify {
    
    [self getVerifyWithMobileNum:self.mobileNum];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refreshBtn1:) userInfo:nil repeats:YES];
    [timer fire];
}

- (void)refreshBtn1:(NSTimer *)timer
{
    [self refreshBtnWithTimer:timer button:self.oldGetVerifyBtn];
}

- (void)refreshBtnWithTimer:(NSTimer *)timer button:(UIButton *)btn
{
    NSInteger i = btn.tag;
    btn.enabled = NO;
    btn.backgroundColor = [UIColor lightGrayColor];
    [btn setTitle:[NSString stringWithFormat:@"剩余%zd秒", i] forState:UIControlStateDisabled];
    if (i == 0) {
        btn.enabled = YES;
        btn.backgroundColor = DBGold;
        [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
        btn.tag = 59;
        [timer invalidate];
        return;
    }
    i--;
    btn.tag = i;
}
上一篇下一篇

猜你喜欢

热点阅读