CNLiveLoginInputView.m 7.99 KB
//
//  CNLiveLoginInputView.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/20.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "CNLiveLoginInputView.h"

@interface CNLiveLoginInputView()<UITextFieldDelegate>
@property (nonatomic, strong) UIImageView *picImage;
@property (nonatomic, strong, readwrite) UITextField *textField;
@property (nonatomic, strong) UIButton *show;
@property (nonatomic, strong) UIImageView *line;
@property (nonatomic, assign) CNLiveButtonType type;


@end
@implementation CNLiveLoginInputView
- (instancetype)initWithPic:(NSString *)pic placeholder:(NSString *)placeholder type:(CNLiveButtonType)type{
    self = [super init];
    if(self){
        _type = type;
        [self setUpUI];
        _picImage.image = [UIImage imageNamed:pic];
        _textField.placeholder = placeholder;
        
        switch (type) {
            case CNLiveButtonTypeNormal:
            {
                _show.hidden = YES;
            }
                break;
            case CNLiveButtonTypeTextEntry:
            {
                _show.hidden = NO;
                _textField.secureTextEntry = YES;
//                [_show setTitle:CustomStr(@"Show") forState:UIControlStateNormal];
//                [_show setTitle:CustomStr(@"Hide") forState:UIControlStateSelected];
                [_show setImage:[UIImage imageNamed:@"cnlive_show"] forState:UIControlStateNormal];
                [_show setImage:[UIImage imageNamed:@"cnlive_hide"] forState:UIControlStateSelected];
                [_show addTarget:self action:@selector(clickedShowBtn:) forControlEvents:UIControlEventTouchUpInside];
            }
                break;
            case CNLiveButtonTypeCode:
            {
                _show.hidden = NO;
                [_show setTitleColor:HEXCOLOR(0xc81e19) forState:UIControlStateNormal];
                _show.layer.borderColor = HEXCOLOR(0xc81e19).CGColor;
                [_show setTitle:CustomStr(@"GetCode") forState:UIControlStateNormal];
                [_show addTarget:self action:@selector(clickedCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countdown) name:GetCodeSuccessful object:nil];
            }
                break;
                
            default:
                break;
        }

    }
    return self;
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    
    _picImage = [[UIImageView alloc] init];
    _picImage.contentMode = UIViewContentModeCenter;
    _picImage.layer.masksToBounds = YES;
    [self addSubview:_picImage];

    _textField = [[UITextField alloc]init];
    _textField.delegate = self;
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.font = [UIFont systemFontOfSize:15];
    [_textField setValue:HEXCOLOR(0xebebeb) forKeyPath:@"_placeholderLabel.textColor"];
    [self addSubview:_textField];
    
    _show = [UIButton buttonWithType:UIButtonTypeCustom];
    _show.backgroundColor = [UIColor whiteColor];
//    _show.layer.masksToBounds = YES;
//    _show.layer.cornerRadius = 4.0f;
//    _show.layer.borderColor = HEXCOLOR(0x333333).CGColor;
//    [_show setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
//    _show.layer.borderWidth = 1.0f;
//    _show.highlighted = NO;
//    _show.titleLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:_show];
    
    _line = [[UIImageView alloc] init];
    _line.backgroundColor = Color_Line;
    [self addSubview:_line];
    
}

- (void)layoutSubviews{
    
    [_picImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).with.offset(10);
        make.left.equalTo(self.mas_left).with.offset(10);
        make.width.offset(20);
        make.height.offset(20);

    }];
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).with.offset(0);
        make.bottom.equalTo(self.mas_bottom).with.offset(0);
        make.left.equalTo(self->_picImage.mas_right).with.offset(10);
        make.right.equalTo(self.mas_right).with.offset(-10-40);
        
    }];

    switch (_type) {
        case CNLiveButtonTypeNormal:
        {
            _show.hidden = YES;
        }
            break;
        case CNLiveButtonTypeTextEntry:
        {
            [_show mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).with.offset(8);
                make.bottom.equalTo(self.mas_bottom).with.offset(-8);
                make.right.equalTo(self.mas_right).with.offset(-10);
                make.width.offset(40);
                
            }];
        }
            break;
        case CNLiveButtonTypeCode:
        {
            _show.layer.masksToBounds = YES;
            _show.layer.cornerRadius = 4.0f;
            _show.layer.borderColor = HEXCOLOR(0x333333).CGColor;
            [_show setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
            _show.layer.borderWidth = 1.0f;
            _show.highlighted = NO;
            _show.titleLabel.font = [UIFont systemFontOfSize:12];
            [_show mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top).with.offset(8);
                make.bottom.equalTo(self.mas_bottom).with.offset(-8);
                make.right.equalTo(self.mas_right).with.offset(-10);
                make.width.offset(70);
                
            }];
        }
            break;
            
        default:
            break;
    }
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).with.offset(-1);
        make.height.offset(0.5);
        make.left.equalTo(self.mas_left).with.offset(0);
        make.right.equalTo(self.mas_right).with.offset(0);

    }];
    
    
}
#pragma mark - Action
- (void)clickedShowBtn:(UIButton *)sender{
    //避免明文/密文切换后光标位置偏移
    self.textField.enabled = NO;    // the first one;
    self.textField.secureTextEntry = sender.selected;
    sender.selected = !sender.selected;
    self.textField.enabled = YES;  // the second one;
    [self.textField becomeFirstResponder]; // the third one
    sender.backgroundColor = [UIColor whiteColor];

}
- (void)clickedCodeBtn:(UIButton *)sender{
    self.block?self.block():@"";
}
- (void)countdown{
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束,关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮的样式
                [self.show setTitle:@"重新发送" forState:UIControlStateNormal];
                [self.show setTitleColor:HEXCOLOR(0x333333) forState:UIControlStateNormal];
                self.show.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [self.show setTitle:[NSString stringWithFormat:@"%.2ds", seconds] forState:UIControlStateNormal];
                [self.show setTitleColor:HEXCOLOR(0x979797) forState:UIControlStateNormal];
                self.show.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{

}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end