简单实现支付密码输入框 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地址 http://files.cnblogs.com/files/sixindev/SafeInpuTextFiled.zip

时间: 2024-08-07 21:17:52

简单实现支付密码输入框 By HL的相关文章

Android支付密码输入框

现在很多应用都会集成支付功能,不管是直接调用支付SDK还是自己平台的虚拟货币,支付密码都是很重要的.一般的应用会直接使用Edittext作为密码输入框,这个看着就有点low了,高大上一点的就会自定义一个支付界面,然后输入效果也会有相应的设计,比如我们看到的支付宝和微信支付就是这样的.因为常用所以我也就简单的写了一个类似的支付密码输入控件,望以后不再纠结于此事,哈哈哈,先来张效果图: 控件就是这样的,具体和怎样的支付界面或对话框搭配,就可以灵活使用了. 编程写代码的快慢和Bug的多少都取决于编程思

(转载)Android支付宝支付封装代码

Android支付宝支付封装代码 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-12-22我要评论 这篇文章主要介绍了Android支付宝支付封装代码,Android支付的时候肯定会使用支付宝进行支付,封装可以简化操作步骤,感兴趣的小伙伴们可以参考一下 在做Android支付的时候肯定会用到支付宝支付, 根据官方给出的demo做起来非常费劲,所以我们需要一次简单的封装. 封装的代码也很简单,就是将官网给的demo提取出一个类来方便使用. ? 1 2 3 4 5 6 7 8

微信支付与支付宝钱包的竞争分析

NO1: 十九世纪七十年代起,“物竞天择,适者生存,优胜劣汰”已逐渐成为现代生物学的口号.而今,不知不觉中,它似乎也成了当代社会学的口号.罗素说:“竞争一直是,甚至从人类起源起就是对大部分激烈活动的剌激物.”所谓“长江后浪推前浪”,在人类资讯的迅速积累之下,如果不能追上时代,自然就要被淘汰了.竞争,已经成了当代社会政治经济发展的重要基础与必然趋势. 从远古时期的以物换物,到后来货币的出现,直到宋朝时第一张纸币“交子”问世,随着经济的不断发展,货币的形式也在不断地变化着. 2003年10月18日,

公众号微信支付

1.概要 公众号是以微信用户的一个联系人形式存在的,支付是微信服务号的核心一环. 本篇主要介绍微信支付这一功能,避免大家再跳微信支付的坑. 1.1 关于Magicodes.WeChat.SDK MAGICODES.WECHAT.SDK为心莱团队封装的轻量级微信SDK,现已全部开源,开源库地址为:https://github.com/xin-lai/Magicodes.WeChat.SDK 更多介绍,请关注后续博客. 2.微信公众号支付 用户已有商城网址,用户通过微信消息.微信扫描二维码.微信自定

第三方支付之微信支付(扫码支付)

第一步:注册微信支付账户,开通扫码支付 具体流程请参照官方说明 第二步:创建Maven项目 1. 添加微信支付SDK依赖.二维码工具依赖(微信支付需要自己通过二维码工具生成支付二维码) <!-- 微信支付 --> <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</ver

如何调通微信支付及微信发货通知接口(Js API)

微信支付提供了一个支付测试页面,微信支付正式使用需要测通支付.发货通知接口 .告警接口.维权接口.告警接口.维权接口非常简单.支付界面调通也相对简单,主要是发货通知接口稍微复杂一点.调通发货通知接口需要注意以下几点: (1) 微信支付文档中提到发货通知接口的PostData,这个其实不是一个form里的一项,其实 PostData的提法有点误导,理解为json串就可以了. (2)以下的写法是错误的: <form name="form2" target="_blank&q

第三方支付之支付宝(电脑网站支付)

第一步:蚂蚁金服开放平台注册账号 该步骤的详细流程请参考蚂蚁金服官方说明,本示例主要关注Java后台代码的实现 第二步:下载SDK,安装到本地或远程Maven仓库 1. 进入下载的SDK的如下目录 alipay.trade.page.pay-JAVA-UTF-8\WebContent\WEB-INF\lib 2. 安装alipay-sdk-javaXXX.jar mvn install:install-file -Dfile=D:\alipay.trade.page.pay-JAVA-UTF-8

移动支付的安全问题

移动支付行为是基于手机号上绑定的银行卡.信用卡以及与商家之间完成,或者基于手机SIM卡与POS 机近距离完成.因此类似于密码破解.信息复制.病毒感染等都有可能对移动支付造成重大的损失. 1. 移动支付中安全问题现状 移动支付中可能的安全问题如下: 1.1.普通手机通常没有加密 普通手机通常没有加密技术,在支付过程中往往会容易造成信息泄露,这已成为移动支付发展的一大难题.用户在使用手机进行支付时,未进行加密等安全措施保护,而黑客们通过钓鱼网站或木马程序就可以窃取用户信息,将被移动支付功能进行非法复

Paypal 支付功能的 C# .NET / JS 实现

说明 最近用到了 Paypal 支付功能,英语一般般的我也不得不硬着头皮踩一踩这样的坑.经过近乎半个月的作,终于实现了简单的支付功能,那么首先就说说使用 Paypal 必定要知道的几点(当前日期 2018年08月07日): 1. 你应该知道 Paypal 支付功能是支持银联卡的,但是不支持中国买家账号支付给中国卖家账号 2. Paypal 接口有两套,切记,产品环境和 sandbox 测试环境不同 3. 测试账号同样不能使用中国账号给中国账号付款 4. 如果你仅仅想具有固定金额的支付按钮,用你的