美文网首页
iOS - 实现六位数字密码

iOS - 实现六位数字密码

作者: Mn_Su | 来源:发表于2017-10-26 16:54 被阅读0次
IMG_1781.PNG

一、初始化弹窗视图

    1.自定义阴影背景视图

        1)新建一个类 ,继承于UIView的类 MSUShadowView

        2)MSUShadowView.h 中内容

            #import <UIKit/UIKit.h>
    
            @interface MSUShadowView : UIView
            
            @end    

        3)MSUShadowView.m 中内容,

            #import "MSUShadowView.h"

            @implementation MSUShadowView
            
            - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
            //    self.hidden = YES;
                for (UIView *view in self.subviews) {
                    CGPoint point = [[touches anyObject] locationInView:view];
                    // 判断点击是否在子视图中,如果不在 隐藏阴影自身视图、子视图、键盘
                    if (![view.layer containsPoint:point]) {
                        self.hidden = YES;
                        view.hidden = YES;
                        
                        // 支付页面 防止点击view移除键盘
                        [self endEditing:YES];
                    }
                }
            }
            @end

        4)懒加载阴影视图
        
            @property (nonatomic , strong) MSUShadowView *shadowView;

            - (MSUShadowView *)shadowView{
                if (!_shadowView) {
                    self.shadowView = [[MSUShadowView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
                    _shadowView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
                    [self.view addSubview:_shadowView];
                }
                return _shadowView;
            }

    2.自定义 键盘视图

        1)申明相关属性

            @property (nonatomic , strong) UIView *pwdView;
            @property (nonatomic , strong) MSUTextField *pwdTF;
            @property (nonatomic , copy) NSString *pwdStr;
            @property (nonatomic , strong) NSMutableArray *pwdBtnArr;

        2)懒加载键盘视图

            /* 同 MSUPutMoneyController */
            -     (UIView *)pwdView{
                    if (!_pwdView) {
                        self.pwdView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-130, WIDTH, 130)];
                        _pwdView.backgroundColor = HEXCOLOR(0xf4f4f4);
                        [self.shadowView addSubview:_pwdView];
                        
                        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                        closeBtn.frame = CGRectMake(14, 20, 20, 20);
                        [closeBtn setImage:[MSUPathTools showImageWithContentOfFileByName:@"search-close"] forState:UIControlStateNormal];
                        closeBtn.adjustsImageWhenHighlighted = NO;
                        [_pwdView addSubview:closeBtn];
                        [closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                        
                        UILabel *topLab = [[UILabel alloc] init];
                        topLab.frame = CGRectMake(WIDTH*0.5-70, 23, 140, 14);
                        topLab.text = @"请输入钱包支付密码";
                        topLab.textColor = HEXCOLOR(0x333333);
                        topLab.font = [UIFont systemFontOfSize:14];
                        [_pwdView addSubview:topLab];
                        
                        self.pwdTF = [[MSUTextField alloc] init];
                        //    _pwdTF.backgroundColor  = [UIColor clearColor];
                        _pwdTF.frame = CGRectMake(14, 60, WIDTH-28, 45);
                        _pwdTF.textColor = HEXCOLOR(0xf4f4f4);
                        _pwdTF.keyboardType = UIKeyboardTypeNumberPad;
                        _pwdTF.delegate = self;
                        [_pwdView addSubview:_pwdTF];
                        [_pwdTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
                        [_pwdTF becomeFirstResponder];
                        
                        //    MSUTextField *msu = [[MSUTextField alloc] init];
                        //    msu.delegate = self;
                        
                        UIView *fuView = [[UIView alloc] init];
                        fuView.frame = CGRectMake(14, 60, WIDTH-28, 45);
                        fuView.backgroundColor = HEXCOLOR(0xf4f4f4);
                        [_pwdView addSubview:fuView];
                        
                        CGFloat wid = (WIDTH-50-11*5)/6;
                        for (NSInteger i = 0; i < 6; i++) {
                            UILabel *pwdLab = [[UILabel alloc] init];
                            pwdLab.frame = CGRectMake(25+(wid+11)*i, 60, wid, wid);
                            pwdLab.textAlignment = NSTextAlignmentCenter;
                            pwdLab.backgroundColor = HEXCOLOR(0xffffff);
                            pwdLab.layer.borderWidth = 1;
                            pwdLab.layer.borderColor = HEXCOLOR(0xc3c3c3).CGColor;
                            pwdLab.textColor = HEXCOLOR(0x333333);
                            pwdLab.font = [UIFont systemFontOfSize:14];
                            [_pwdView addSubview:pwdLab];
                            [self.pwdBtnArr addObject:pwdLab];
                        }
                        
                        UIButton *privateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                        privateBtn.frame = CGRectMake(0, HEIGHT-130, WIDTH, 130);
                        privateBtn.backgroundColor = [UIColor clearColor];
                        privateBtn.adjustsImageWhenHighlighted = NO;
                        [_pwdView addSubview:privateBtn];
                        _pwdView.hidden = YES;
                
                    }
                    return _pwdView;
                }
                
                - (NSMutableArray *)pwdBtnArr{
                    if (!_pwdBtnArr) {
                        self.pwdBtnArr = [NSMutableArray array];
                    }
                    return _pwdBtnArr;
                }

二、点击按钮 ,显示视图

    - (void)sureBtnClick:(UIButton *)sender{
         self.shadowView.hidden = NO;
        self.pwdView.hidden = NO;
        [self.pwdTF becomeFirstResponder];
        
        // 显示视图时候,默认清空相关状态
        self.pwdStr = @"";
        self.pwdTF.text = @"";
        for (UILabel *lab in self.pwdBtnArr) {
            lab.text = @"";
        }
    }   

三、监听textfield 相关代理和方法

1)输入框点击方法
    #pragma mark -
- (void)textFieldDidChange:(UITextField *)textField{
    if (textField == self.pwdTF) {
        if (textField.text.length > 0 && textField.text.length < 7) {
            UILabel *label = self.pwdBtnArr[textField.text.length-1];
            label.text = @"●";
            self.pwdStr = textField.text;
            if (self.pwdStr.length == 6) {
                // 数据请求相关处理
    }    

}

2)删除代理,详见另一篇博客 Textfiled相关
- (void)deleteBtnClick:(UITextField *)textField{
    if (textField == self.pwdTF) {
        NSLog(@"text  -- %@",textField.text);
        if ((textField.text.length > 0 && textField.text.length < 7)|| textField.text.length == 0) {
            UILabel *label = self.pwdBtnArr[textField.text.length];
            label.text = @"";
            self.pwdStr = textField.text;
        }
    }

}


3) 关闭方法

    - (void)closeBtnClick:(UIButton *)sender{
        self.shadowView.hidden = YES;
        [self.pwdTF resignFirstResponder];
    }

相关文章

  • iOS - 实现六位数字密码

    一、初始化弹窗视图 二、点击按钮 ,显示视图 三、监听textfield 相关代理和方法

  • IOS开发-六位密码输入框

    现在app中时常用到六位密码,而这种密码输入框则需要我们自己来编写。 实现 六位密码框并不是一个被分成六格的tex...

  • 给你一个强大的外置大脑,要吗?

    有时候怎么都想不起来密码是什么,而且还是各种不同类型的密码,纯数字,数字加字母,六位,八位,那个时候简直想爆炸。 ...

  • 一串密码的输入既是福也是祸

    一串密码,一串我的密码,你的密码,打开钱包与银行卡的密码 六位密码,可能不值一提,因为它只有六个数字,再怎么排列总...

  • iOS - Password AutoFill

    密码自动填充 Version - - iOS 11.0 简化登陆流程 保存密码,用户不用记住密码 一、实现原理 将...

  • 青春会在哪一刻散场(3)

    我们都为梦想努力奋斗,日复一日,年复一年。我升职了加薪了,银行卡六位数的密码终于迎来了和他一样的数字,我大概实现了...

  • 采集

    采集好数据上传才能得到六位数数字密码,得到密码了才能给空调上电开机。 上次这台空调由于手机登录不上,当时用的临时动...

  • 怎样设置密码安全(day56)

    4--6位数的密码到底安不安全 今天我们的手机密码,银行卡密码大都是只有六位数字,这样到底安不安全,而其它比如微信...

  • 滨州专业技术继续教育

    账号:身份证 密码:后六位

  • 如何在Mac上实现密码可见

    如何在Mac上实现密码可见 在 macOS 上的密码框使用的是 NSSecureTextField 不像在 iOS...

网友评论

      本文标题:iOS - 实现六位数字密码

      本文链接:https://www.haomeiwen.com/subject/cemkpxtx.html