美文网首页
获取验证码

获取验证码

作者: 破夕_____________ | 来源:发表于2017-07-27 15:03 被阅读6次
/** 获取验证码按钮 */
@property (strong, nonatomic) UIButton *getCodeButton;
/** 定时器 */
@property (strong, nonatomic) NSTimer *timer;

/** 验证码倒计时 */
@property (assign, nonatomic) NSInteger second;
//获取验证码
    _getCodeButton = [[UIButton alloc] initWithFrame:CGRectMake(480 * kScale, 294 * kScale, 145 * kScale, 78 * kScale)];
    _getCodeButton.tag = codeType;
    _getCodeButton.titleLabel.numberOfLines = 0;
    _getCodeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"获取验证码"];
    [str addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28 * kScale],
                         NSForegroundColorAttributeName:kRGBA(255, 75, 91, 1)}
                 range:NSMakeRange(0, 5)];
    [_getCodeButton setAttributedTitle:str forState:UIControlStateNormal];
    [_getCodeButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_getCodeButton];
#pragma mark - 按钮点击事件
- (void)buttonAction:(UIButton *)button {
    
    switch (button.tag) {
        case codeType: {
            
            [_getCodeButton setEnabled:NO];
            _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
            
        } break;
            
        case loginType: {
            
            [self releaseTimer];
            
        } break;
            
        default:
            break;
    }
    
}

#pragma mark - 倒计时方法验证码实现倒计时60秒,60秒后按钮变换开始的样子
- (void)timerFireMethod:(NSTimer *)timer {
    
    if (_second == 1) {
        
        [timer invalidate];
        _second = 60;
        [_getCodeButton setEnabled:YES];
        
        //富文本
        NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"重新获取"];
        [str addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28 * kScale],
                             NSForegroundColorAttributeName:kRGBA(255, 75, 91, 1)}
                     range:NSMakeRange(0, 4)];
        [_getCodeButton setAttributedTitle:str forState:UIControlStateNormal];
        
    } else {
        
        _second--;
        
        //富文本
        NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"重新获取\n%ld秒",_second]];
        [str addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28 * kScale],
                             NSForegroundColorAttributeName:kRGBA(255, 75, 91, 1)}
                     range:NSMakeRange(0, 4)];
        [str addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24 * kScale],
                             NSForegroundColorAttributeName:kRGBA(153, 153, 153, 1)}
                     range:NSMakeRange(4, str.length - 4)];
        [_getCodeButton setAttributedTitle:str forState:UIControlStateNormal];
        
    }
    
}

#pragma mark - 如果确认完成,停止验证码倒数
- (void)releaseTimer {
    
    if (_timer) {
        if ([_timer respondsToSelector:@selector(isValid)]) {
            if ([_timer isValid]) {
                [_timer invalidate];
                _second = 60;
                [self.getCodeButton setEnabled:YES];
                //富文本
                NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"获取验证码"];
                [str addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28 * kScale],
                                     NSForegroundColorAttributeName:kRGBA(255, 75, 91, 1)}
                             range:NSMakeRange(0, 5)];
                [_getCodeButton setAttributedTitle:str forState:UIControlStateNormal];
                
            }
        }
    }
    
}


相关文章

网友评论

      本文标题:获取验证码

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