iOS 实现短信验证码方框输入样式带光标闪烁效果

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
typedef void (^ResultBlock)(NSString *str , NSDictionary *dic,BOOL isOK);
@interface StepField : UIView
- (instancetype)initWithFrame:(CGRect)frame;
@property (nonatomic, assign) NSInteger fieldCount;
@property (nonatomic, copy) ResultBlock resultBlock;
@end

@class UITextFieldAddDel;
@protocol TextFieldDelegate <NSObject>
- (void)textFieldDelete:(UITextFieldAddDel *)textField;
@end

@interface UITextFieldAddDel : UITextField
@property (nonatomic, assign) id <TextFieldDelegate> delDelegate;
@end

NS_ASSUME_NONNULL_END

 

#import "StepField.h"

@interface StepField ()
<
TextFieldDelegate
>
@property (nonatomic, strong) NSMutableDictionary *dic;
@property (nonatomic, strong) NSMutableArray *textFieldArr;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) NSInteger fildTextLength;
@end

@implementation StepField

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

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

-(void)setFieldCount:(NSInteger)fieldCount
{
    _fieldCount = fieldCount;
    [self initUI];
}

-(void)initUI
{
    for (UIView *view in self.subviews)
    {
        [view removeFromSuperview];
    }
    _fildTextLength = 1;
    self.textFieldArr = [self addMenuButton:_fieldCount];
}


-(NSMutableArray *)addMenuButton:(NSInteger)count
{
    NSMutableArray *arrReturn = [NSMutableArray array];
    CGFloat screenW =  CGRectGetWidth(self.bounds);
    CGFloat space = 0;
    CGFloat x = 15;
    CGFloat y = 0;
    CGFloat w =  ( screenW - space * 2 - x * ( count + 1 ) ) /  count;
    CGFloat h = self.frame.size.height;
    //    CGFloat bigW = w * 2 + x ;
    CGFloat temp = 0;
    CGFloat newI = 0;
    CGFloat newW = 0;
    
    for (int i = 0;  i < count ; i++)
    {
        space += newW + x;
        newW = w;
        newI = i - temp;
        
        if (newI == 0)
        {
            space = x;
        }
        UITextFieldAddDel *textField = [[UITextFieldAddDel alloc]init];
        textField.borderStyle = UITextBorderStyleLine;
        [textField addTarget:self action:@selector(valueChange:) forControlEvents:(UIControlEventEditingChanged)];
        textField.delDelegate = self;
        textField.frame = CGRectMake(space, y, newW, h);
        [textField setTextAlignment:(NSTextAlignmentCenter)];
        [self addSubview:textField];
        [arrReturn addObject:textField];
    }
    return arrReturn;
}


-(void)valueChange:(UITextField *)textfield
{
    // 当前输入框的下标
    NSInteger currentIndex = 0;
    for (NSInteger i = 0; i< self.textFieldArr.count; i++)
    {
        UITextField *fild = self.textFieldArr[i];
        
        if (fild == textfield)
        {
            currentIndex = i;
        }
        
        // 以下标未key 输入框内容为value 做键对存储
        NSString *key = [NSString stringWithFormat:@"%zd",i];
        NSString *value ;
        
        if (fild.text.length > 0)
        {
            value = fild.text;
        }
        else
        {
            value = @" ";
        }
        
        [self.dic setValue:value forKey:key];
    }
    
    // 从字典中取数所有的输入框内容
    NSString *strAll = [NSString string];
    for (NSInteger i = 0; i< self.textFieldArr.count-1; i++)
    {
        NSString *key = [NSString stringWithFormat:@"%zd",i];
        NSString *value = [_dic valueForKey:key];
        strAll = [strAll stringByAppendingString:value];
    }
    
    // 判断输入内容 如果当前输入框内容为空用空格代替 更新对应的字典
    NSString *key = [NSString stringWithFormat:@"%zd",currentIndex];
    
    if (textfield.text.length > 0)
    {
        if (textfield.text.length > _fildTextLength)
        {
            NSRange range = NSMakeRange(textfield.text.length - _fildTextLength, _fildTextLength);
            textfield.text = [textfield.text substringWithRange:range];
        }
        [self.dic setObject:textfield.text forKey:key];
    }
    else
    {
        [self.dic setObject:@" " forKey:key];
    }
    
    self.text = strAll;
    // 更新当前需要编辑的输入框
    UITextField *fild;
    if ( currentIndex + 1 > self.textFieldArr.count -1 )
    {
        fild = self.textFieldArr.lastObject;
    }
    else
    {
        if (self.text.length > strAll.length)
        {
            if (currentIndex >= self.textFieldArr.count-1)
            {
                fild  = self.textFieldArr[currentIndex];
            }
            else
            {
                fild  = self.textFieldArr[currentIndex+1];
            }
        }
        else
        {
            if (currentIndex < 0)
            {
                fild  = self.textFieldArr[0];
            }
            else
            {
                fild  = self.textFieldArr[currentIndex+1];
            }
        }
    }
    [fild becomeFirstResponder];
    
    if (self.resultBlock)
    {
        NSArray *arr = [_dic allValues];
        BOOL isOK = [arr containsObject:@" "];
        
        isOK = !isOK;
        
        self.resultBlock(self.text,self.dic,isOK);
    }
}

- (void)textFieldDelete:(UITextFieldAddDel *)textField
{
//    NSLog(@"删除 = %@",textField.text);
    if (textField.text.length == 0)
    {
        // 找到当前编辑的输入框的下标
        NSInteger currentindex=0;
        for (int i = 0;  i < self.textFieldArr.count; i++)
        {
            UITextFieldAddDel *fild = self.textFieldArr[i];
            
            if (fild == textField)
            {
                currentindex = i;
                break;
            }
        }
        
        if (currentindex <= 0)
        {
            UITextFieldAddDel *fild = self.textFieldArr.firstObject;
            [fild becomeFirstResponder];
        }
        else
        {
            UITextFieldAddDel *fild = self.textFieldArr[currentindex-1];
            [fild becomeFirstResponder];
        }
    }
    
    // 当前输入框的下标
    NSInteger currentIndex = 0;
    for (NSInteger i = 0; i< self.textFieldArr.count; i++)
    {
        UITextField *fild = self.textFieldArr[i];
        
        if (fild == textField)
        {
            currentIndex = i;
        }
        
        // 以下标未key 输入框内容为value 做键对存储
        NSString *key = [NSString stringWithFormat:@"%zd",i];
        NSString *value ;
        
        if (fild.text.length > 0)
        {
            value = fild.text;
        }
        else
        {
            value = @" ";
        }
        
        [self.dic setValue:value forKey:key];
    }
    
    // 从字典中取数所有的输入框内容
    NSString *strAll = [NSString string];
    for (NSInteger i = 0; i< self.textFieldArr.count-1; i++)
    {
        NSString *key = [NSString stringWithFormat:@"%zd",i];
        NSString *value = [_dic valueForKey:key];
        strAll = [strAll stringByAppendingString:value];
    }
    
    // 判断输入内容 如果当前输入框内容为空用空格代替 更新对应的字典
    NSString *key = [NSString stringWithFormat:@"%zd",currentIndex];
    
    if (textField.text.length > 0)
    {
        if (textField.text.length > _fildTextLength)
        {
            NSRange range = NSMakeRange(textField.text.length - _fildTextLength, _fildTextLength);
            textField.text = [textField.text substringWithRange:range];
        }
        [self.dic setObject:textField.text forKey:key];
    }
    else
    {
        [self.dic setObject:@" " forKey:key];
    }
    
    self.text = strAll;
    if (self.resultBlock)
    {
        NSArray *arr = [_dic allValues];
        BOOL isOK = [arr containsObject:@" "];
        isOK = !isOK;
        self.resultBlock(self.text,self.dic,isOK);

    }
}

-(NSMutableDictionary *)dic{
    if (!_dic)
    {
        _dic = [NSMutableDictionary dictionary];
    }
    return _dic;
}

-(NSString *)text{
    if (!_text) {
        _text = [NSString string];
    }
    return _text;
}


@end


@implementation UITextFieldAddDel

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (void)deleteBackward {
    [super deleteBackward];
    
    //    !!!这里要调用super方法,要不然删不了东西
    if ([self.delDelegate respondsToSelector:@selector(textFieldDelete:)])
    {
        [self.delDelegate textFieldDelete:self];
    }
    
}

@end
// 调用
    StepField *file = [[StepField alloc]initWithFrame:CGRectMake(0, 88, self.view.frame.size.width, 50)];
    file.fieldCount = 5;
    file.resultBlock = ^(NSString * _Nonnull str, NSDictionary * _Nonnull dic, BOOL isOK) {
        
        NSLog(@"%@",str);
//        NSLog(@"%@",dic);
        
        NSLog(@"%d",isOK);
        
    };
    [self.view addSubview:file];

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值