简单实现支付密码输入框 By HL
密码输入框在微信,支付宝中比较常见
主要功能点
1.6位(或者N位)密码输入框封装
// SBTextFieldAlert.h
/**
* 密码输入框封装
*/ #import <UIKit/UIKit.h> @interface SBPwdTextField : UIView<UITextFieldDelegate> @property(nonatomic,strong) UITextField *passwordTextField; - (void)clearUpPassword; @end
//
// SBTextFieldAlert.m
// SafeInpuTextFiled
#define kDotSize CGSizeMake (10.0f,10.0f) //密码点的大小
#define kDotCount 6 //密码点数 #import "SBPwdTextField.h" @implementation SBPwdTextField
{
NSMutableArray *passwordIndicatorArrary;
} -(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) { self.layer.borderWidth=1;
self.layer.borderColor=[UIColor lightGrayColor].CGColor; [self addSubview:self.passwordTextField];
[self initPwdTextField];
}
return self;
} -(UITextField *)passwordTextField
{
if (!_passwordTextField) {
_passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0,50, 30)];
_passwordTextField.hidden = YES;
_passwordTextField.delegate = self;
_passwordTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
_passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
[_passwordTextField addTarget:self action:@selector(PwdTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:_passwordTextField];
}
return _passwordTextField;
} -(void)PwdTextFieldDidChange:(UITextField *)tf
{
NSLog(@"%@",tf.text);
[self setDotWithCount:tf.text.length];
} -(void)initPwdTextField
{
//每个密码输入框的宽度
CGFloat width = self.bounds.size.width/kDotCount; //生成分割线
for (int i = 0; i < kDotCount-1; i++)
{
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake((i + 1) * width, 0, 0.5f, self.bounds.size.height)];
lineImageView.backgroundColor = [UIColor grayColor];
[self addSubview:lineImageView];
} passwordIndicatorArrary=[[NSMutableArray alloc]init];
//生成中间的点
for (int i = 0; i < kDotCount; i++)
{
UIImageView *dotImageView = [[UIImageView alloc] initWithFrame:CGRectMake((width - kDotSize.width) / 2.0f + i * width, (self.bounds.size.height - kDotSize.height) / 2.0f, kDotSize.width, kDotSize.height)];
dotImageView.backgroundColor = [UIColor blackColor];
dotImageView.layer.cornerRadius = kDotSize.width / 2.0f;
dotImageView.clipsToBounds = YES;
dotImageView.hidden = YES; //先隐藏
[self addSubview:dotImageView]; [passwordIndicatorArrary addObject:dotImageView];
} } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:@"\n"])
{
//按回车关闭键盘
[textField resignFirstResponder];
return NO;
}
else if(string.length == 0)
{
//判断是不是删除键
return YES;
}
else if(textField.text.length >= kDotCount)
{
//输入的字符个数大于6,则无法继续输入,返回NO表示禁止输入
NSLog(@"输入的字符个数大于6,忽略输入");
return NO;
}
else
{
return YES;
}
} /**
* 重置显示的点
*/
- (void)setDotWithCount:(NSInteger)count
{
for (UIImageView *dotImageView in passwordIndicatorArrary)
{
dotImageView.hidden = YES;
} for (int i = 0; i< count; i++)
{
((UIImageView*)[passwordIndicatorArrary objectAtIndex:i]).hidden = NO;
}
if (count== kDotCount) {
NSLog(@"输入完毕");
[[NSNotificationCenter defaultCenter] postNotificationName:@"uuuu" object:self.passwordTextField.text];
}
} /**
* 清除密码
*/
- (void)clearUpPassword
{
_passwordTextField.text = @"";
[self setDotWithCount:_passwordTextField.text.length];
} /**
* 点击self 弹出键盘
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_passwordTextField becomeFirstResponder];
} - (void)dealloc
{
[_passwordTextField removeFromSuperview];
_passwordTextField=nil;
}
2.显示提示框封装
// SBInputTextAlert.h
// SafeInpuTextFiled #import <UIKit/UIKit.h>
#import "SBPwdTextField.h" @interface SBInputTextAlert : UIView @property(nonatomic,strong) UILabel *titleLabel; /**
* 密码输入框
*/
@property(nonatomic,strong) SBPwdTextField *TextFieldAlert; -(void)show;
-(void)dismiss; @end @interface SBCoverMask : UIView
+(void)maskViewShow;
+(void)maskViewDismiss;
@end
// SBInputTextAlert.m
// SafeInpuTextFiled
#import "SBInputTextAlert.h"
#define SBKeyWindow [UIApplication sharedApplication].keyWindow @interface SBInputTextAlert () @property(nonatomic,strong) UIView *linView; @end @implementation SBInputTextAlert -(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
self.layer.cornerRadius=8;
self.layer.masksToBounds=YES; [self initSubViews];
[self addSubview:self.titleLabel];
[self addSubview:self.linView]; [self addSubview:self.TextFieldAlert];
}
return self;
} -(UIView *)linView
{
if (!_linView) {
_linView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame)+10, self.bounds.size.width,0.5)];
_linView.backgroundColor=[UIColor greenColor];
}
return _linView;
} -(void)initSubViews
{
UIButton *closeButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
[closeButton setTitle:@"关闭" forState:UIControlStateNormal];
closeButton.backgroundColor=[UIColor greenColor];
[closeButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
} -(UILabel *)titleLabel
{
if (!_titleLabel) {
_titleLabel=[[UILabel alloc]init];
_titleLabel.textAlignment=NSTextAlignmentCenter;
_titleLabel.font=[UIFont boldSystemFontOfSize:18];
_titleLabel.frame=CGRectMake(0,5, self.bounds.size.width,25);
}
return _titleLabel;
} -(SBPwdTextField *)TextFieldAlert
{
if (!_TextFieldAlert) {
CGFloat textFieldwidth=(self.bounds.size.width-40);
_TextFieldAlert=[[SBPwdTextField alloc]initWithFrame:CGRectMake(20, 50 ,textFieldwidth,textFieldwidth/6)];
}
return _TextFieldAlert;
} /**
* 显示这儿view
*/
-(void)show{
[SBCoverMask maskViewShow];
self.frame=CGRectMake(0,-100, self.bounds.size.width, self.bounds.size.height);
[UIView animateWithDuration:0.3 animations:^{
self.hidden = NO;
self.frame=CGRectMake(SBKeyWindow.frame.size.width/2-self.bounds.size.width/2,100, self.bounds.size.width, self.bounds.size.height);
[self.TextFieldAlert becomeFirstResponder];
[SBKeyWindow addSubview:self];
}];
}
/**
* 隐藏这个view
*/
-(void)dismiss{
[SBCoverMask maskViewDismiss];
[UIView animateWithDuration:0.5 animations:^{
self.hidden = YES;
[self.TextFieldAlert resignFirstResponder];
[self removeFromSuperview];
}]; //清空密码
[self.TextFieldAlert clearUpPassword];
} @end /**
* 蒙版MaskView
*/
@implementation SBCoverMask
+(void)maskViewShow{
SBCoverMask *view = [[SBCoverMask alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view.backgroundColor = [UIColor blackColor];
view.alpha = 0.3; [[UIApplication sharedApplication].keyWindow addSubview:view];
}
+(void)maskViewDismiss{
UIWindow *keyWidow=[UIApplication sharedApplication].keyWindow;
for (UIView *view in keyWidow.subviews) {
if ([view isKindOfClass:[SBCoverMask class]]) {
[view removeFromSuperview];
}
}
} @end
使用
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *CSButton=[UIButton buttonWithType:UIButtonTypeCustom];
CSButton.frame=CGRectMake(50,350, 100, 80);
CSButton.backgroundColor=[UIColor redColor];
[CSButton setTitle:@"显示输入框" forState:UIControlStateNormal];
[CSButton addTarget:self action:@selector(showKeyBord) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:CSButton];
UIButton *CSButton2=[UIButton buttonWithType:UIButtonTypeCustom];
CSButton2.frame=CGRectMake(self.view.frame.size.width-100-50,350, 100, 80);
CSButton2.backgroundColor=[UIColor redColor];
[CSButton2 setTitle:@"隐藏输入框" forState:UIControlStateNormal];
[CSButton2 addTarget:self action:@selector(hideKeyBord) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:CSButton2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoNextVC:) name:@"uuuu" object:nil];
}
-(SBInputTextAlert *)SBTextField{
if (!_SBTextField) {
_SBTextField=[[SBInputTextAlert alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width-20,200)];
_SBTextField.titleLabel.text=@"请输入6位支付密码";
_SBTextField.backgroundColor=[UIColor whiteColor];
}
return _SBTextField;
}
-(void)gotoNextVC:(NSNotification *)nti
{
NSLog(@"text=%@",nti.object);
[self.SBTextField dismiss];
secondViewController *secvc=[[secondViewController alloc]init];
[self presentViewController:secvc animated:YES completion:nil];
}
-(void)showKeyBord
{
[self.SBTextField show];
[self.SBTextField.TextFieldAlert.passwordTextField becomeFirstResponder];
}
-(void)hideKeyBord
{
[self.SBTextField dismiss];
[self.SBTextField.TextFieldAlert.passwordTextField resignFirstResponder];
}
效果图 可根据设计图添加相关UI即可

Demo地址 https://files.cnblogs.com/files/sixindev/SafeInpuTextFiled.zip
简单实现支付密码输入框 By HL的更多相关文章
- Android 支付密码输入框,自定义EditText实现密码输入框功能;
刚撸出来的密码输入框,注释和逻辑看着挺清晰的,一些属性还没有添加,下个博客把属性添加上去: 看一下图: 直接看代码吧! import android.content.Context; import a ...
- springboot使用自定义注解和反射实现一个简单的支付
优点: 未使用if else,就算以后增加支付类型,也不用改动之前代码 只需要新写一个支付类,给添加自定义注解@Pay 首先: 定义自定义注解 Pay 定义 CMBPay ICBCPay 两种支付 根 ...
- (转载)Android支付宝支付封装代码
Android支付宝支付封装代码 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-12-22我要评论 这篇文章主要介绍了Android支付宝支付封装代码,Android支付的时候 ...
- Paypal 支付功能的 C# .NET / JS 实现
说明 最近用到了 Paypal 支付功能,英语一般般的我也不得不硬着头皮踩一踩这样的坑.经过近乎半个月的作,终于实现了简单的支付功能,那么首先就说说使用 Paypal 必定要知道的几点(当前日期 20 ...
- 微信小程序支付c#后台实现
今天为大家带来比较简单的支付后台处理 首先下载官方的c#模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在WxPayAPI项目目录中添加两个“一般处理程序” (改名为GetOp ...
- 分享一个PC端六格密码输入框写法
如图.我们一般做商城类的项目不免会用到支付密码输入框,我研究了下并决定发上来,也当作是自己成长路上的一点小小的记录.本次介绍的是基于vue的项目 html: <template> < ...
- 信小程序支付(C#后台+前台)
今天为大家带来比较简单的支付后台处理 首先下载官方的c#模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在WxPayAPI项目目录中添加两个“一般处理程序” (改名为GetOp ...
- php对微信支付回调处理的方法(合集)
支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答. 对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽 ...
- ThinkPHP中实现微信支付(jsapi支付)流程
https://blog.csdn.net/sinat_35861727/article/details/72783988 之前写过一篇文章讲了 PHP实现微信支付(jsapi支付)流程 ,详见文章: ...
随机推荐
- Java二、八、十、十六进制介绍
1.说明 在Java中整数有四种表示方式, 分别为十进制,二进制,八进制,十六进制, 其中十进制就是平常最熟悉,使用最多的进制: 二进制是在计算机中使用最多的进制, 八进制和十六进制都是基于二进制的, ...
- 【Spring专场】「IOC容器」不看源码就带你认识核心流程以及运作原理
这是史上最全面的Spring的核心流程以及运作原理的分析指南 [Spring核心专题]「IOC容器篇」不看繁琐的源码就带你浏览Spring的核心流程以及运作原理 [Spring核心专题]「AOP容器篇 ...
- Pytest_Hook函数pytest_addoption(parser):定义自己的命令行参数(14-1)
考虑场景: 我们的自动化用例需要支持在不同测试环境运行,有时候在dev环境运行,有时候在test环境运行: 有时候需要根据某个参数不同的参数值,执行不同的业务逻辑: 上面的场景我们都可以通过" ...
- Flask_安装和配置(一)
安装Flask pip install flask 一 .创建Flask项目 Flask与Django相比,没有提供任何自动创建项目的操作,所以需要手动创建项目及启动项目的管理文件 例如,创建项目目录 ...
- 阿里云服务器 配置 tomcat 发布spring boot项目 的具体操作 【使用公网ip】
1.前言 spring boot 转成war包 后用tomcat发布的具体操作在我另一篇随笔有详细记载,不论是window系统还是Linux系统,tomcat的发布配置都是一样的,所以这里不具体讲这个 ...
- layui父表单获取子表单的值完成修改操作
最近在做项目时,学着用layui开发后台管理系统. 但在做编辑表单时遇到了一个坑. 点击编辑时会出现一个弹窗. 我们需要从父表单传值给子表单.content是传值给子表单 layer.open({ t ...
- 关于Java注释报错的一个问题
做作业时发现一个问题,注释会报错 主要代码如下: String str = "C:\\users\\Default.migrated"; str += "\\HelloW ...
- 华为HMS Core全新推出会员转化&留存预测模型
现在,付费学知识,付费听歌,付费看电视剧,付费享受线上购物优惠--等等场景已经成为大部分年轻人的日常. 而对于企业商家来说,付费会员作为企业差异化用户运营的手段,不仅有利于提升用户的品牌忠诚度,在当下 ...
- 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...
- 给自己的网站装上SSL证书
给网站装上SSL证书 前言 主要是因为自己的阿里云快过期了,自己的博客也重新用了一下Halo,重新安装SSL的时候有些地方忘了,所以在此留个记录! 关于SSL 阮一峰<图解图解SSL/TLS协议 ...