效果图:

用到的类:

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. Kettle能做什么?

    简介 Kettle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,绿色无需安装,数据抽取高效稳定. Kettle 中文名称叫水壶,该项目的主程序员MATT  ...

  2. U3D常用题

    最先执行的方法是:1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearDo ...

  3. webpack之proxyTable配置

    有两种情况: 一,有统一的项目名 config下index.js 配置如下: proxyTable: { '/tbt_user':{ target: 'http://47.98.216.251:80' ...

  4. 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置

    前几天看到了有人在android5.0上实现了如下图一样的效果,我自己就去搜了下.参考了国外一篇文章和国内的一篇文章,最终实现了想要的效果.具体参考的网址我已经贴到文章末尾,大家可以去英文的那个网站看 ...

  5. 10.18正式开发stark组件*(三)

    2018-10-18 19:15:54 等这个stark组件做完了再上传到github上面,然后再整理博客!这就到周末啦! 因为models导入的时候出现bug,所以只有源码没有测试数据! 源码都有注 ...

  6. (广度搜索)A - Prime Path(11.1.1)

    A - Prime Path(11.1.1) Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64 ...

  7. ASP.NET Core 实现用户登录验证的最低配置

    背景是在一个项目中增加临时登录功能,只需验证用户是否登录即可,所需的最低配置与实现代码如下. 在 Startup 的 ConfigureServices() 方法中添加 Authentication ...

  8. 1.7Oob 继承关系中构造方法的使用

    1:父类中最好要有一个空参数的构造方法,因为默认的构造方法在自定义了构造方法后就不存在了,需要显示的写出来. 若父类中没有空参数的构造方法,则子类必须有自定义的构造方法,且用super()调用父类的构 ...

  9. React中Props 和 State用法

    React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...

  10. MVC模型和MVT模型

    MVC 大部分开发平台都需要搭建的后台框架,Java和PHP最为喜爱 M:model.     模型.      主要用于对数据库层的封装 V:view.        视图.      用于对用户展 ...