美文网首页
iOS获取验证码倒计时

iOS获取验证码倒计时

作者: Torin76 | 来源:发表于2017-10-09 17:04 被阅读200次

    开始计时调用下面这个方法就可以了。获取验证码的按钮就会开始显示倒计时直到60s后恢复最初状态

    [self startTime];
    
    实现代码如下:

    定义定时器和时间

    @implementation LoginViewController {
        NSTimer *_timer;
        int _waiTime;
    }
    
    #pragma mark--开始计时
    - (void)startTime {
        
        [_timer invalidate];
        _waiTime = 60;
        [self updateVareyButton];
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(updateWaitTime)
                                                userInfo:nil
                                                 repeats:YES];
    }
    
    - (void)updateVareyButton {
        
        NSString *title = @"获取验证码";
        if (_waiTime > 0) {
            title = [NSString stringWithFormat:@"%d秒后重发", _waiTime];
            [self.getVerfyCodeBtn setTitleColor:SecondColor forState:UIControlStateNormal];
            self.getVerfyCodeBtn.userInteractionEnabled = NO;
        }
        [self.getVerfyCodeBtn setTitle:title forState:UIControlStateNormal];
    }
    
    - (void)updateWaitTime {
        
        _waiTime = _waiTime - 1;
        [self updateVareyButton];
        if (_waiTime <= 0) {
            [self.getVerfyCodeBtn setTitleColor:MainColor forState:UIControlStateNormal];
            self.getVerfyCodeBtn.userInteractionEnabled = YES;
            [_timer invalidate];
            _timer = nil;
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:iOS获取验证码倒计时

          本文链接:https://www.haomeiwen.com/subject/ccypyxtx.html