OC分割输入验证码的视觉效果
效果图:

用到的类:

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分割输入验证码的视觉效果的更多相关文章
- js实现输入验证码
html部分: <div> <input type="text" id="input" /> <input type=" ...
- iOS学习——输入验证码界面封装
在很多App中都有输入验证码的功能需求,最近项目需要也有这个功能.做完之后简单整理了一下,将实现的基本思路做下记录.实现后的效果大致如下图所示,当四位签到码全部输入时,提交按钮是可以提交的,否则提交按 ...
- Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组
// 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 // @function [parent=#string] split // @param string input 输入字符串 // ...
- JS中同步显示并分割输入的数字字符串
题目比较晦涩,来张图来说明要表达的效果: 第一张图的效果就是,用户输入一个数字,上面就显示一个大层,然后显示输入的数字,并把数字用空格按照每四位分割出来.好像在建行的网上银行上面就有这种效果.第二个图 ...
- python语言验证码识别,以后不用老输入验证码了。
1.Python 3.6 安装包 1.要加环境变量 2.pip安装PIL库 3.pip安装pytesseract模块 2.tesseract-ocr-setup-4.00.00dev.exe -- ...
- 使用request实现手工输入验证码登录
我们的目标网站是这个http://awehome.com.cn,登录页面是这个http://awehome.com.cn/tenant/login import requests import jso ...
- Python输错4次用户名密码需要输入验证码
time = 0 login_success = False USER_NAME = "alex" PWD = "alex123" CHECK_CODE = & ...
- PHP中判断输入验证码是否一致
首先用session将随机生成的验证码的值传到页面,然后获取当前文本框中输入的值 进行对比:代码如下: 生成的随机数,把它传到session里面 <? session_start(); 必 ...
- shiro 和spring集合 实现登录时输入验证码并校验(七)
编写实现验证码的主体实现类:CaptchaCode import java.util.UUID; import javax.servlet.http.HttpServletRequest; impor ...
随机推荐
- SQL导出数据到EXCEL的问题
DTS导出向导 不会 我这有个是用C#语言写的 try { Excel.Application xApp = new Excel.ApplicationClass(); xApp.Visible = ...
- 基金 、 社保和QFII等机构的重仓股排名评测
来源:基金前20大重仓股持仓股排名 基金前15大重仓股持仓股排名 基金重仓前15大个股,相较于同期沪深300的平均收益, 近1月:2.23%, 近3月:3.09%, 近6月:9.61%, 近1年:13 ...
- 自己开发chrome插件生成二维码
摘要: 最近在开发微信项目时,需要在微信调试,所以经常会在微信中输入本地服务地址,输入起来特别麻烦,所以自己就想了想微信中的扫一扫,然后开发了这款chrome插件,将当前url生成二维码,用微信扫一扫 ...
- [OpenCV] Install OpenCV 3.3 with DNN
OpenCV 3.3 Aug 3, 2017 OpenCV 3.3 has been released with greatly improved Deep Learning module and l ...
- [Laravel] 09 - Functional models
Laravel框架下的若干常用功能实现. 文件上传 邮件发送 缓存使用 错误日志 队列应用 文件上传 一.配置文件 功能 配置 [config/filesystems.php] 'disks' =&g ...
- U3D对齐功能
1,按快捷键Shift + v 的用法: [1]先选中你要对齐的模型A,[将物体A对齐到物体B]. [2]按快捷键Shift + v ,此时将鼠标移到模型A的各个顶点处,可发现各个顶点上会 ...
- linux下通过curl访问web服务器
在通过xshell或者其他远程连接工具连接linux服务器,没安装浏览器,却要测试web服务的请求: 可以使用curl 访问web服务器 例如返回百度的主页内容 #curl www.baidu.com ...
- html学习_认识html
1.HTML骨架 <html>----根标签 <head>---头标签 </head> <body>---主体标签 </body> < ...
- {python之IO多路复用} IO模型介绍 阻塞IO(blocking IO) 非阻塞IO(non-blocking IO) 多路复用IO(IO multiplexing) 异步IO(Asynchronous I/O) IO模型比较分析 selectors模块
python之IO多路复用 阅读目录 一 IO模型介绍 二 阻塞IO(blocking IO) 三 非阻塞IO(non-blocking IO) 四 多路复用IO(IO multiplexing) 五 ...
- 如何从视频中分离出音乐,和对音乐做分割,合并的处理(瑞典音乐家-新八宝盒.mp3.rar下载)
点击下载:瑞典音乐家-新八宝盒.mp3.rar 1.工具 Total Video Converter 3.71 视频处理(安装软件,有绿色版的) MP3剪切器 MP3剪切器(小工具) Mp3mateP ...