效果图:

用到的类:

UITextField+VerCodeTF.h

#import <UIKit/UIKit.h>
@protocol VerCodeTFDelegate <UITextFieldDelegate> @optional
- (void)textFieldDidDeleteBackward:(UITextField *)textField;
@end NS_ASSUME_NONNULL_BEGIN @interface UITextField (VerCodeTF)
@property (weak, nonatomic) id <VerCodeTFDelegate> delegate;
@end NS_ASSUME_NONNULL_END

UITextField+VerCodeTF.m

#import "UITextField+VerCodeTF.h"
#import <objc/runtime.h> @implementation UITextField (VerCodeTF) + (void)load {
Method method1 = class_getInstanceMethod([self class], NSSelectorFromString(@"deleteBackward"));
Method method2 = class_getInstanceMethod([self class], @selector(vc_deleteBackward));
method_exchangeImplementations(method1, method2);
} /**
当删除按钮点击是触发的事件
*/
- (void)vc_deleteBackward {
[self vc_deleteBackward]; if ([self.delegate respondsToSelector:@selector(textFieldDidDeleteBackward:)])
{
id <VerCodeTFDelegate> delegate = (id<VerCodeTFDelegate>)self.delegate;
[delegate textFieldDidDeleteBackward:self];
} } @end

VerificationCodeView.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface VerificationCodeView : UIView

@end

NS_ASSUME_NONNULL_END

VerificationCodeView.m

#import "VerificationCodeView.h"
#import "UITextField+VerCodeTF.h"
#define Count 4 //一共有多少个输入框
#define Secure NO //是否密文
#define Width 34 //输入框的宽度,这边我比较懒,都做成正方形了 //输入框输入时边框颜色
#define RedColor [UIColor redColor].CGColor
//输入框未输入时边框颜色
#define GrayColor [UIColor grayColor].CGColor @interface VerificationCodeView ()<UITextFieldDelegate>
@property(nonatomic,strong)NSMutableArray *tfArr;
@property(nonatomic,copy)NSString *lastTFText;//最有一个TextField的内容 @end
@implementation VerificationCodeView -(instancetype)initWithFrame:(CGRect)frame{
frame.size.height = 34;
self = [super initWithFrame:frame];
if(self){
self.lastTFText = @"";
self.tfArr = [NSMutableArray array]; float margin = ( frame.size.width - Width * Count)/3.0;
for(int i=0;i<Count;i++){
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake((Width+margin)*i, 0, Width, Width)];
tf.secureTextEntry = Secure;
tf.tag = i+1;
tf.layer.borderWidth = 1.0f;
tf.layer.cornerRadius = 5.0f;
tf.textAlignment = NSTextAlignmentCenter;
tf.keyboardType = UIKeyboardTypeNumberPad;
tf.delegate = self; [self addSubview:tf];
[self.tfArr addObject:tf];
if(i == 0){
[tf becomeFirstResponder];
tf.userInteractionEnabled = YES;
tf.layer.borderColor = RedColor;
}else{
tf.userInteractionEnabled = NO;
tf.layer.borderColor = GrayColor;
}
[tf addTarget:self action:@selector(tfChange:) forControlEvents:UIControlEventEditingChanged];
}
}
return self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//>0说明我输入了一个字符
if(textField.text.length > 0){
textField.text = [textField.text substringToIndex:textField.text.length-1];
}
return YES;
}
-(void)tfChange:(UITextField *)textField{ if(textField.tag == Count){
self.lastTFText = textField.text;
} if(textField.text.length > 0){
if(textField.tag < self.tfArr.count){
UITextField *tf = self.tfArr[textField.tag];
tf.userInteractionEnabled = YES;
[tf becomeFirstResponder];
tf.layer.borderColor = RedColor;
textField.userInteractionEnabled = NO;
}else{
//四个输入框输入完毕,
// [self endEditing:YES];
} }
} - (void)textFieldDidDeleteBackward:(UITextField *)textField{
if(textField.tag == Count && self.lastTFText.length > 0){
[textField becomeFirstResponder];
self.lastTFText = @"";
}else{
//因为第一个UITextField的tag值为1
if(textField.tag > 1){
UITextField *tf = self.tfArr[textField.tag-2];
tf.userInteractionEnabled = YES;
tf.text = @"";
[tf becomeFirstResponder];
textField.userInteractionEnabled = NO;
textField.layer.borderColor = GrayColor;
}
}
}
@end
 

 使用:

VerificationCodeView *codeView = [[VerificationCodeView alloc]initWithFrame:CGRectMake(50, 150, self.view.bounds.size.width-100, 44)];
[self.view addSubview:codeView];

OC分割输入验证码的视觉效果的更多相关文章

  1. js实现输入验证码

    html部分: <div> <input type="text" id="input" /> <input type=" ...

  2. iOS学习——输入验证码界面封装

    在很多App中都有输入验证码的功能需求,最近项目需要也有这个功能.做完之后简单整理了一下,将实现的基本思路做下记录.实现后的效果大致如下图所示,当四位签到码全部输入时,提交按钮是可以提交的,否则提交按 ...

  3. Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组

    // 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 // @function [parent=#string] split // @param string input 输入字符串 // ...

  4. JS中同步显示并分割输入的数字字符串

    题目比较晦涩,来张图来说明要表达的效果: 第一张图的效果就是,用户输入一个数字,上面就显示一个大层,然后显示输入的数字,并把数字用空格按照每四位分割出来.好像在建行的网上银行上面就有这种效果.第二个图 ...

  5. python语言验证码识别,以后不用老输入验证码了。

    1.Python 3.6 安装包 1.要加环境变量 2.pip安装PIL库 3.pip安装pytesseract模块 2.tesseract-ocr-setup-4.00.00dev.exe   -- ...

  6. 使用request实现手工输入验证码登录

    我们的目标网站是这个http://awehome.com.cn,登录页面是这个http://awehome.com.cn/tenant/login import requests import jso ...

  7. Python输错4次用户名密码需要输入验证码

    time = 0 login_success = False USER_NAME = "alex" PWD = "alex123" CHECK_CODE = & ...

  8. PHP中判断输入验证码是否一致

    首先用session将随机生成的验证码的值传到页面,然后获取当前文本框中输入的值  进行对比:代码如下: 生成的随机数,把它传到session里面 <? session_start();   必 ...

  9. shiro 和spring集合 实现登录时输入验证码并校验(七)

    编写实现验证码的主体实现类:CaptchaCode import java.util.UUID; import javax.servlet.http.HttpServletRequest; impor ...

随机推荐

  1. SpringBatch的初步了解

    一.SpringBatch是一个批处理的框架,作为一个Spring组件,提供了通过使用Spring的依赖注入来处理批处理的条件. 什么是批处理呢? 在现代企业应用当中,面对复杂的业务以及海量的数据,除 ...

  2. 仿迅雷播放器教程 -- C++界面制作方法的对比 (9)

        上一个教程对比的5个方向共7个界面框架,都是非常权威,应用很广泛的库,绝对是非常稳定,并且能够做出常见的界面出来,可以放心大胆的用在项目里.     但那7个界面框架再好,也总是没有绝对的优势 ...

  3. iOS mac终端下的SQL语句

    本文转载至 http://blog.csdn.net/majiakun1/article/details/41281801 我们都知道数据库的创建可以借助图形化的数据库工具软件,但也可以在Mac终端下 ...

  4. Servlet知识点回顾

    一.Servlet生命周期 服务器调用一个Servlet的8个步骤: 1.在服务器启动时,当Servlet被配置好或者被客户首次请求时,由服务器加载servlet,这一步相当于下列代码: Class ...

  5. MySQL主从同步技术

    一,先装mysql 准备两台服务器,主服务器是10.0.0.200 副服务器是10.0.0.128主服务器当作mysql的服务端,下载mysql-serveryum install mysql-ser ...

  6. Nordic NRF51822 从零开始系列(外部设备片—MPU6050DMP驱动的移植)

    一.硬件准备             (1)开发板和软件参看 Nordic NRF51822 从零开始系列(一)开发环境的搭建             (2)mpu6050模块 二.前置知识     ...

  7. 秒秒钟提高办公技巧的6个Excel技巧

    一.职工身份证号码是否登记重复(=IF(COUNTIF(B2:B13,B2&"*")>1,"重复","")) 职工列表人数众多 ...

  8. hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

    Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. C# 各类常见Exception 异常信息

    一直对报错有些迷惑的地方,什么时候try,catch那些Exception更合适,报错信息更能快速定位问题所在... 转载链接← 正文 Exception: 所有异常对象的基类. SystemExce ...

  10. idea 乱码问题

    1. db browser查询结果为乱码: 找到idea的安装目录 如C:\..\Roaming\JetBrains\IntelliJ IDEA Community Edition 2018.1.3\ ...