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 ...
随机推荐
- JSON 转JAVA代码
http://jsongen.byingtondesign.com/ http://pojo.sodhanalibrary.com/ http://www.jsonschema2pojo.org/
- 修改torndb库为依赖pymysql,使其适应python3,一个更简单的操作数据库的类。
1.python的MySQLdb和pymysql是两个基本数据库操作包,MySQLdb安装很麻烦,要有c++相关环境,python3也安装不了. python3一般安装pymysql,此包与MySQL ...
- [CNN] Understanding Convolution
From: http://blog.csdn.net/zouxy09/article/details/49080029 一个概念需经过反复的推敲以及时间的沉淀,之后才能真正理解 [OpenCV] Im ...
- 采用镜像的方法安装python第三方库
转自:https://blog.csdn.net/s740556472/article/details/68557330 pip install --index https://pypi.mirror ...
- MySQL 去除字段中的换行和回车符
今天csv 导入关键词的时候遇到问题 字段结束会有 回车符号 解决方法: ), ),''); char(10): 换行符 char(13): 回车符
- Automatic Summarization of Bug Reports
CONTENT: example : KDE bug report: https://bugs.kde.org/show_bug.cgi?id=188311 (其中还有很多comments没显示) ...
- 一个表中的id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数
一个表中的id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数 select id ,Count(*) from table_name group by id having count( ...
- 配置nginx实现windows/iis应用负载均衡
nginx是俄罗斯人开发的一款跨平台的高性能HTTP和反向代理服务器,可以利用它实现web应用服务器的负载均衡. 反向代理是指将用户请求通过代理服务器转发给后端内部网络的应用服务器,典型的应用比如配置 ...
- 【CF566C】Logistical Questions 点分
[CF566C]Logistical Questions 题意:给你一棵n个点的树,点有点权,边有边权,两点间的距离为两点间的边权和的$3\over 2$次方.求这棵树的带权重心. $n\le 200 ...
- day_11py学习
''' 字典增加和删除 1.添加 xxx[新的key] = value 2.删除 del xxx[key] 3.修改 xxx[已存在的key] = new_value 4.查询 xxx.get(key ...