美文网首页
仿写美团带光标的验证码输入框

仿写美团带光标的验证码输入框

作者: 秋叶红90 | 来源:发表于2020-12-03 13:37 被阅读0次
Dec-03-2020 13-43-25.gif

#import <UIKit/UIKit.h>

typedef void(^MQTextViewBlock)(NSString *text);

@interface MQVerCodeInputView : UIView

@property (nonatomic, assign) UIKeyboardType keyBoardType;
@property (nonatomic, copy) MQTextViewBlock block;

/*验证码的最大长度*/
@property (nonatomic, assign) NSInteger maxLenght;

/*未选中下的view的borderColor*/
@property (nonatomic, strong) UIColor *viewColor;

/*选中下的view的borderColor*/
@property (nonatomic, strong) UIColor *viewColorHL;
/// 风格类型 1 是默认的 使用xib 或者 storyboard
@property (nonatomic, assign) IBInspectable NSInteger styleType;


-(void)mq_verCodeViewWithMaxLenght;

@end



#import "MQVerCodeInputView.h"
#import "Masonry.h"

@interface MQVerCodeInputView ()<UITextViewDelegate>

@property (nonatomic, strong) UIView *contairView;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) NSMutableArray *viewArr;
@property (nonatomic, strong) NSMutableArray *labelArr;
@property (nonatomic, strong) NSMutableArray *pointlineArr;




@end

@implementation MQVerCodeInputView

//verView.maxLenght = 6;//最大长度
//verView.keyBoardType = UIKeyboardTypeNumberPad;
//[verView mq_verCodeViewWithMaxLenght];

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

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initDefaultValue];
    }
    return self;
}


-(void)initDefaultValue{
    //初始化默认值
    self.maxLenght = 6;
    _viewColor = [UIColor colorWithRed:228/255.0 green:228/255.0 blue:228/255.0 alpha:1];
    _viewColorHL = [UIColor colorWithRed:255/255.0 green:70/255.0 blue:62/255.0 alpha:1];
    self.backgroundColor = [UIColor clearColor];
    [self beginEdit];
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.styleType == 1) {
        self.maxLenght = 6; //最大长度
        self.keyBoardType = UIKeyboardTypeNumberPad;
        [self mq_verCodeViewWithMaxLenght];
    }
}


-(void)mq_verCodeViewWithMaxLenght{
    if (_contairView != nil) {
        return;
    }
    //创建输入验证码view
    if (_maxLenght<=0) {
        return;
    }
    if (_contairView) {
        [_contairView removeFromSuperview];
    }
    _contairView  = [UIView new];
    _contairView.backgroundColor = [UIColor clearColor];
    [self addSubview:_contairView];
    [_contairView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.mas_height);
        make.centerX.equalTo(self);
        make.centerY.equalTo(self);
    }];
    [_contairView addSubview:self.textView];
    //添加textView
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_contairView);
    }];
    CGFloat w = 46;
//    CGFloat padding = (CGRectGetWidth(self.frame) -_maxLenght*w)/(_maxLenght - 1);
    CGFloat padding = 8;
    UIView *lastView;
    for (int i=0; i<self.maxLenght; i++) {
        UIView *subView = [UIView new];
        //#4B2BD3 rgba(75,43,211,1)
        subView.backgroundColor = [UIColor colorWithRed:75/255.0 green:43/255.0 blue:211/255.0 alpha:1];
        subView.layer.cornerRadius = 12;
        
        subView.layer.borderWidth = 0.5;
        subView.userInteractionEnabled = NO;
        [_contairView addSubview:subView];
        [subView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (lastView) {
                make.left.equalTo(lastView.mas_right).with.offset(padding);
            }else{
                make.left.equalTo(@(0));
            }
            make.centerY.equalTo(self.contairView);
            make.height.equalTo(self.contairView.mas_height);
            make.width.equalTo(@(w));
            
        }];
        UILabel *subLabel = [UILabel new];
        subLabel.textColor = [UIColor whiteColor];
        subLabel.font = [UIFont systemFontOfSize:38];
        [subView addSubview:subLabel];
        [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(subView);
            make.centerY.equalTo(subView);
        }];
        lastView = subView;
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake((w-2)/2,5,2,(CGRectGetHeight(self.frame)-10))];
        CAShapeLayer *line = [CAShapeLayer layer];
        line.path = path.CGPath;
        line.fillColor =  _viewColorHL.CGColor;
        [subView.layer addSublayer:line];
        if (i == 0) {//初始化第一个view为选择状态
            [line addAnimation:[self opacityAnimation] forKey:@"kOpacityAnimation"];
            line.hidden = NO;
            subView.layer.borderColor = _viewColorHL.CGColor;
        }else{
            line.hidden = YES;
            subView.layer.borderColor = _viewColor.CGColor;
        }
        [self.viewArr addObject:subView];
        [self.labelArr addObject:subLabel];
        [self.pointlineArr addObject:line];
    }
    [_contairView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastView?lastView.mas_right:@(0));
    }];
}

#pragma mark - TextView

-(void)beginEdit{
    [self.textView becomeFirstResponder];
}

-(void)endEdit{
    [self.textView resignFirstResponder];
}

- (void)textViewDidChange:(UITextView *)textView{
    
    NSString *verStr = textView.text;
    //有空格去掉空格
    verStr = [verStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (verStr.length >= _maxLenght) {
        verStr = [verStr substringToIndex:_maxLenght];
        [self endEdit];
    }
    textView.text = verStr;

    if (self.block) {
        //将textView的值传出去
        self.block(verStr);
    }

    for (int i= 0; i < self.viewArr.count; i++) {
        //以text为中介区分
        UILabel *label = self.labelArr[i];
        if (i<verStr.length) {
            [self changeViewLayerIndex:i pointHidden:YES];
            label.text = [verStr substringWithRange:NSMakeRange(i, 1)];

        }else{
            [self changeViewLayerIndex:i pointHidden:i==verStr.length?NO:YES];
            if (!verStr&&verStr.length==0) {//textView的text为空的时候
                [self changeViewLayerIndex:0 pointHidden:NO];
            }
            label.text = @"";
        }
    }
}
- (void)changeViewLayerIndex:(NSInteger)index pointHidden:(BOOL)hidden{
    
    UIView *view = self.viewArr[index];
    view.layer.borderColor = hidden?_viewColor.CGColor:_viewColorHL.CGColor;
    CAShapeLayer *line =self.pointlineArr[index];
    if (hidden) {
        [line removeAnimationForKey:@"kOpacityAnimation"];
    }else{
        [line addAnimation:[self opacityAnimation] forKey:@"kOpacityAnimation"];
    }
    line.hidden = hidden;

}

- (CABasicAnimation *)opacityAnimation {
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = @(1.0);
    opacityAnimation.toValue = @(0.0);
    opacityAnimation.duration = 0.9;
    opacityAnimation.repeatCount = HUGE_VALF;
    opacityAnimation.removedOnCompletion = YES;
    opacityAnimation.fillMode = kCAFillModeForwards;
    opacityAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    return opacityAnimation;
}


#pragma mark --setter&&getter
// 1 2 3 4 5 6 123456
-(void)setMaxLenght:(NSInteger)maxLenght{
    _maxLenght = maxLenght;
}

-(void)setKeyBoardType:(UIKeyboardType)keyBoardType{
    _keyBoardType = keyBoardType;
    self.textView.keyboardType = keyBoardType;
}

-(void)setViewColor:(UIColor *)viewColor{
    _viewColor = viewColor;
}

-(void)setViewColorHL:(UIColor *)viewColorHL{
    _viewColorHL = viewColorHL;
}

-(UITextView *)textView{
    if (!_textView) {
        _textView = [UITextView new];
        _textView.tintColor = [UIColor clearColor];
        _textView.backgroundColor = [UIColor clearColor];
        _textView.textColor = [UIColor clearColor];
        _textView.delegate = self;
        _textView.keyboardType = UIKeyboardTypeDefault;
    }
    return _textView;
}

-(NSMutableArray *)pointlineArr{
    if (!_pointlineArr) {
        _pointlineArr = [NSMutableArray new];
    }
    return _pointlineArr;
}
-(NSMutableArray *)viewArr{
    if (!_viewArr) {
        _viewArr = [NSMutableArray new];
    }
    return _viewArr;
}
-(NSMutableArray *)labelArr{
    if (!_labelArr) {
        _labelArr = [NSMutableArray new];
    }
    return _labelArr;
}

@end

使用方法 storyboard 上画一个view 然后继承与他,接着更改属性styleType = 1 就可以了

``
block 在代码里实现

``

相关文章

网友评论

      本文标题:仿写美团带光标的验证码输入框

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