iOS - 实现六位数字密码

2017-10-26  本文已影响0人  Mn_Su
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];
    }
上一篇下一篇

猜你喜欢

热点阅读