iOS 仿京东6位密码支付输入框

news/2024/7/4 18:07:58

开发需求中有时候我们需要用于安全支付的功能, 需要设置APP钱包的支付密码, 页面是仿照京东的6位输入框的的做法, 效果如下图:

看起来是有由6个UITextField组成, 其实并不是,这只是一个假象.

####实现思路:

  1. 创建一个UITextField,仅仅一个而不是六个! 然后用5根竖线进行分割,这样我们看到的就是一个有6个等同输入框的视图.
  2. 创建黑点可以通过创建一个正方形的UIView,设置圆角为宽高的一半,就是一个圆了,使其 frame 显示在中间则黑点居中即可.
  3. 当点击输入时候使用shouldChangeCharactersInRange 方法来用来输入的 textfield 做处理, 是否成为第一响应者,用来用户输入, 监听其值的改变.
  4. 当密码的长度达到需要的长度时,关闭第一响应者. 这里可以使用 block 来传递 password 的值.
  5. 提供一个清除 password 的方法

现在核心代码如下: ####先抽出加密支付页面 ZLSafetyPswView, 在.m中主要就是实现页面的效果:

#define kDotSize CGSizeMake (10, 10) // 密码点的大小
#define kDotCount 6  // 密码个数
#define K_Field_Height self.frame.size.height  // 每一个输入框的高度等于当前view的高度

@interface ZLSafetyPswView () <UITextFieldDelegate>

// 密码输入文本框
@property (nonatomic, strong) UITextField *pswTextField;
// 用于存放加密黑色点
@property (nonatomic, strong) NSMutableArray *dotArr;

@end
复制代码

创建分割线和黑点.

#pragma mark - 懒加载

- (NSMutableArray *)dotArr {
    if (!_dotArr) {
        _dotArr = [NSMutableArray array];

    }
    return _dotArr;
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupWithPswTextField];
    }
    return self;
}

- (void)setupWithPswTextField {
    
    // 每个密码输入框的宽度
    CGFloat width = self.frame.size.width / kDotCount;
    
    // 生成分割线
    for (int i = 0; i < kDotCount - 1; i++) {
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.pswTextField.frame) + (i + 1) * width, 0, 1, K_Field_Height)];
        lineView.backgroundColor = [UIColor grayColor];
        [self addSubview:lineView];
    }
    
    self.dotArr = [[NSMutableArray alloc] init];
    
    // 生成中间的黑点
    for (int i = 0; i < kDotCount; i++) {
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.pswTextField.frame) + (width - kDotCount) / 2 + i * width, CGRectGetMinY(self.pswTextField.frame) + (K_Field_Height - kDotSize.height) / 2, kDotSize.width, kDotSize.height)];
        dotView.backgroundColor = [UIColor blackColor];
        dotView.layer.cornerRadius = kDotSize.width / 2.0f;
        dotView.clipsToBounds = YES;
        dotView.hidden = YES; // 首先隐藏
        [self addSubview:dotView];
        
        // 把创建的黑色点加入到存放数组中
        [self.dotArr addObject:dotView];
    }
}
复制代码

创建一个UITextField.切记输入的文字颜色和输入框光标的颜色为透明!

#pragma mark - init

- (UITextField *)pswTextField {
    
    if (!_pswTextField) {
        _pswTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, K_Field_Height)];
        _pswTextField.backgroundColor = [UIColor clearColor];
        // 输入的文字颜色为无色
        _pswTextField.textColor = [UIColor clearColor];
        // 输入框光标的颜色为无色
        _pswTextField.tintColor = [UIColor clearColor];
        _pswTextField.delegate = self;
        _pswTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _pswTextField.keyboardType = UIKeyboardTypeNumberPad;
        _pswTextField.layer.borderColor = [[UIColor grayColor] CGColor];
        _pswTextField.layer.borderWidth = 1;
        [_pswTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:_pswTextField];
    }
    return _pswTextField;
}
复制代码

文本框内容改变时,用来用户输入, 监听其值的改变.

#pragma mark - 文本框内容改变

/**
 *  重置显示的点
 */
- (void)textFieldDidChange:(UITextField *)textField {
    
    NSLog(@"目前输入显示----%@", textField.text);
    
    for (UIView *dotView in self.dotArr) {
        dotView.hidden = YES;
    }
    for (int i = 0; i < textField.text.length; i++) {
        ((UIView *)[self.dotArr objectAtIndex:i]).hidden = NO;
    }
    if (textField.text.length == kDotCount) { 
        NSLog(@"---输入完毕---");
        
        [self.pswTextField resignFirstResponder];
    }
    
    // 获取用户输入密码
    !self.passwordDidChangeBlock ? : self.passwordDidChangeBlock(textField.text);
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSLog(@"输入变化%@", 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;
    }
}
复制代码

清除密码时收起键盘并将文本输入框值置为空.

#pragma mark - publick method

/**
 *  清除密码
 */
- (void)clearUpPassword {
    [self.pswTextField resignFirstResponder];
    self.pswTextField.text = nil;
    [self textFieldDidChange:self.pswTextField];
}
复制代码

####接着在当前所需控制器里,创建支付页面并拿到用户输入密码去做支付相关逻辑处理

// 加密支付页面
    ZLSafetyPswView *pswView = [[ZLSafetyPswView alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width - 100, 45)];
    [self.view addSubview:pswView];
    self.pswView = pswView;
    pswView.passwordDidChangeBlock = ^(NSString *password) {
        NSLog(@"---用户输入密码为: %@",password);
    };
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor orangeColor];
    button.frame = CGRectMake(100, 280, self.view.frame.size.width - 200, 50);
    [button addTarget:self action:@selector(clearPsw) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"清空密码" forState:UIControlStateNormal];
    [self.view addSubview:button];
复制代码

方便测试加上清空密码按钮

// 清空密码
- (void)clearPsw {
    
    [self.pswView clearUpPassword];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
复制代码

我这里是做6位支付密码的, 你同样可以修改kDotCount密码个数值,目前也有4位的.


http://www.niftyadmin.cn/n/1897064.html

相关文章

[15/N]论得趣

2019独角兽企业重金招聘Python工程师标准>>> 一日即终 恍若怅失 细点清单 险忘曰趣 急急参上 神奇心流 婴孩常有 总能重回 入后难出 也是妄然 甘若涌流 一但弹出 怨尤郁结 冲击作习 纷乱不堪宛如毒品 上瘾难逃 不计后果 只求沉浸 初心易忘 故曰心流 只当工具 能入能…

全栈工程师必备!Devops Tools 周期表

老外整理的 Devops Tools 周期表&#xff0c;可以用酷炫屌炸天形容&#xff0c;划分了数据库、CI、日志、安全、监控、配置管理、云服务等15个大类&#xff0c;120个工具。有很人多都没全听说过&#xff0c;你要是全学会了你就是全栈工程师! 点击这里查看原文&#xff0c;最好用…

递归遍历循环删除 一些特定目录

为什么80%的码农都做不了架构师&#xff1f;>>> 循环删除 一些目录 import os; import shutil;curpath os.getcwd(); print "curpath is " curpath print os.listdir(curpath) # find cocosstudio targettpath curpath "/" "cocosst…

三菱plcascll转换16进制_三菱PLC的6大软元件知识盘点,收干货啦~

点击↑↑技成培训 &#xff0c;关注并置顶即可长期免费订阅17万工控人关注的微信平台&#xff1a;技术分享、学习交流、工控视频今天跟大家分享的是关于PLC软元件的详细用处。(三菱系列)一、状态继电器(S)状态继电器是编制步进程序的重要软元件&#xff0c;与辅助继电器一样&am…

Python中time日期、时间格式转换

在我们日常的数据采集中&#xff0c;时间time的重要性毋庸置疑的&#xff0c;因为它是衡量采集到的信息是否具有正确时效性的唯一因素。如果我们采集到的信息对于客户来说已经是过期的信息&#xff0c;这对于客户没有使用的价值&#xff0c;影响客户使用。 所以我们今天就来说说…

mybatis if test 相等的情况怎样动态拼接sql

今天程序须要依据前台的传过来的状态推断在数据库里是取 where a>b 还是 a<b 还是 a0 的情况 搞了一下午最后试了下 在if 里面拼接 #{status}#{status} 一切ok了 具体代码例如以下 <if test" status1">and inv.security_inventory < inv.actu…

软件开发软技能:“从无意识的故障中学习”模式

\本文要点\\软技术模式是经证实可解决常见问题的个人和人际交互行为的组合。\\t系统故障几乎不可能完全避免&#xff0c;但同时每次故障也都带来了改进的机会。\\t“从无意识的故障中学习”模式指导我们在故障事件后改进系统的弹性。\\t该模型有四个独立的步骤&#xff1a;识别…