效果图:

用到的类:

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. 多线程开发之一 NSThread

    每个 iOS 应用程序都有个专门用来更新显示 UI 界面.处理用户的触摸事件的主线程,因此不能将其他太耗时的操作放在主线程中执行,不然会造成主线程堵塞(出现卡机现象),带来不好的用户体验. 一般的解决 ...

  2. 仿迅雷播放器教程 -- 提取exe资源(12)

    既然选择了一个界面库,那么咱们就开始吧!     既然是仿迅雷播放器,那当然要把迅雷播放器的资源提取出来啦,但是很多小伙伴可能不知道怎么提取,所以这里就教大家一些方法: 一.传统的资源提取器     ...

  3. electron安装+运行+打包成桌面应用+打包成安装文件+开机自启动

    1.初始化node项目,生成package.json文件 npm init 2.安装electron,并保存为开发依赖项 npm install electron -D 3.根目录下新建index.j ...

  4. Get Started with the Google Fonts API

    Get Started with the Google Fonts API This guide explains how to use the Google Fonts API to add fon ...

  5. Linux环境部署

    1,开机初始化的配置 iptables -F # 清空防火墙 /etc/init.d/iptables stop # 关闭iptables setenforce # 暂停selinux 2,编译安装p ...

  6. 10.9 Xadmin

    2018-10-9 13:53:39

  7. Page Visibility API 页面是否获取焦点 Event: visibilitychange

    W3C 文档 https://www.w3.org/TR/page-visibility/ MDN 文档 https://www.w3.org/TR/page-visibility/ // Docum ...

  8. solus 系统 - 编译安裝 ibus-rime 中文输入法(附:小鹤双拼双形配置文件)

    編譯方法參見官網 - https://github.com/rime/home/wiki/RimeWithIBus 安装依赖:列出几个可能用到的命令 #安裝cmake gcc等开发工具 sudo eo ...

  9. sometimes we should use "disable fork" instead of "disable block_name"

    A disable named block statement stops the execution of all blocks with that same name in all threads ...

  10. 理解 vm.$nextTick

    有同学在看 Vue 官方文档时,对 API 文档中的 Vue.nextTick 和 vm.$nextTick 的作用不太理解. 其实如果看一下深入响应式原理 - vue.js中的有关内容,可能会有所理 ...