123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- //
- // CodeInputView.m
- // JDZBorrower
- //
- // Created by WangXueqi on 2018/4/20.
- // Copyright © 2018年 JingBei. All rights reserved.
- //
- #import "CodeInputView.h"
- #import "CALayer+Category.h"
-
- #define K_Screen_Width [UIScreen mainScreen].bounds.size.width
- #define K_Screen_Height [UIScreen mainScreen].bounds.size.height
- @interface CodeInputView()<UITextViewDelegate>
- @property(nonatomic,strong)UITextView * textView;
- @property(nonatomic,strong)NSMutableArray <CAShapeLayer *> * lines;
- @property(nonatomic,strong)NSMutableArray <UILabel *> * labels;
- @end
- @implementation CodeInputView
- - (instancetype)initWithFrame:(CGRect)frame inputType:(NSInteger)inputNum selectCodeBlock:(SelectCodeBlock)CodeBlock {
- self = [super initWithFrame:frame];
- if (self) {
- self.CodeBlock = CodeBlock;
- self.inputNum = inputNum;
-
- self.K_W = (self.frame.size.width - 25) / self.inputNum;
- [self initSubviews];
- }
- return self;
- }
- - (void)initSubviews {
- CGFloat W = CGRectGetWidth(self.frame);
- CGFloat H = self.K_W; //CGRectGetHeight(self.frame);
- CGFloat Padd = (self.frame.size.width - self.inputNum*self.K_W)/(self.inputNum+1);
- [self addSubview:self.textView];
- self.textView.frame = CGRectMake(Padd, 0, W-Padd*2, H);
- //默认编辑第一个.
- // [self beginEdit];
- for (int i = 0; i < _inputNum; i ++) {
- UIView *subView = [UIView new];
- subView.frame = CGRectMake(Padd+(self.K_W+Padd)*i, (CGRectGetHeight(self.frame) - H) / 2, self.K_W, H);
- subView.userInteractionEnabled = NO;
- [self addSubview:subView];
- [subView layer].borderColor = [[UIColor lightGrayColor] CGColor];
- [subView layer].borderWidth = 1;
- //[CALayer addSubLayerWithFrame:CGRectMake(2, H-2, self.K_W - 4, 2) backgroundColor:[UIColor lightGrayColor] backView:subView];
- //Label
- UILabel *label = [[UILabel alloc]init];
- label.frame = CGRectMake(0, 0, self.K_W, H);
- label.textAlignment = NSTextAlignmentCenter;
- label.textColor = [UIColor darkGrayColor];
- label.font = [UIFont systemFontOfSize:16];
- [subView addSubview:label];
- //光标
- UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(self.K_W / 2, 10, 2, H - 20)];
- CAShapeLayer *line = [CAShapeLayer layer];
- line.path = path.CGPath;
- line.fillColor = [UIColor darkGrayColor].CGColor;
- [subView.layer addSublayer:line];
- if (i == 0) {
- [line addAnimation:[self opacityAnimation] forKey:@"kOpacityAnimation"];
- //高亮颜色
- line.hidden = YES;
- }else {
- line.hidden = YES;
- }
- //把光标对象和label对象装进数组
- [self.lines addObject:line];
- [self.labels addObject:label];
- }
- }
- - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
- {
- [self beginEdit];
- return YES;
- }
- #pragma mark - UITextViewDelegate
- - (void)textViewDidChange:(UITextView *)textView {
- NSString *verStr = textView.text;
- if (verStr.length > _inputNum) {
- textView.text = [textView.text substringToIndex:_inputNum];
- }
- //大于等于最大值时, 结束编辑
- if (verStr.length >= _inputNum) {
- [self endEdit];
- }
- if (self.CodeBlock) {
- self.CodeBlock(textView.text);
- }
- for (int i = 0; i < _labels.count; i ++) {
- UILabel *bgLabel = _labels[i];
-
- if (i < verStr.length) {
- [self changeViewLayerIndex:i linesHidden:YES];
- bgLabel.text = [verStr substringWithRange:NSMakeRange(i, 1)];
- }else {
- [self changeViewLayerIndex:i linesHidden:i == verStr.length ? NO : YES];
- //textView的text为空的时候
- if (!verStr && verStr.length == 0) {
- [self changeViewLayerIndex:0 linesHidden:NO];
- }
- bgLabel.text = @"";
- }
- }
- }
- //设置光标显示隐藏
- - (void)changeViewLayerIndex:(NSInteger)index linesHidden:(BOOL)hidden {
- CAShapeLayer *line = self.lines[index];
- if (hidden) {
- [line removeAnimationForKey:@"kOpacityAnimation"];
- }else{
- [line addAnimation:[self opacityAnimation] forKey:@"kOpacityAnimation"];
- }
- [UIView animateWithDuration:0.25 animations:^{
- line.hidden = hidden;
- }];
- }
- //开始编辑
- - (void)beginEdit{
- NSString *verStr = self.textView.text;
- if(verStr.length == 0)
- {
- self.lines[0].hidden = NO;
- }
- else if(verStr.length < 6)
- {
- self.lines[verStr.length].hidden = NO;
- }
- //[self.textView becomeFirstResponder];
- }
- //结束编辑
- - (void)endEdit{
- for( int i = 0; i < self.lines.count; i++)
- {
- self.lines[i].hidden = YES;
- }
- [self.textView resignFirstResponder];
-
- }
- //闪动动画
- - (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;
- }
- //对象初始化
- - (NSMutableArray *)lines {
- if (!_lines) {
- _lines = [NSMutableArray array];
- }
- return _lines;
- }
- - (NSMutableArray *)labels {
- if (!_labels) {
- _labels = [NSMutableArray array];
- }
- return _labels;
- }
- - (UITextView *)textView {
- if (!_textView) {
- _textView = [UITextView new];
- _textView.tintColor = [UIColor clearColor];
- _textView.backgroundColor = [UIColor clearColor];
- _textView.textColor = [UIColor clearColor];
- _textView.delegate = self;
- _textView.keyboardType = UIKeyboardTypeNumberPad;
- }
- return _textView;
- }
- @end
|