博客> iOS - 实现六位数字密码
iOS - 实现六位数字密码
5分钟前 评论:0 阅读:674 OS_JJ
ios 实现六位数字密码

 IMG_1781.PNG

一、初始化弹窗视图

    1.自定义阴影背景视图

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

        2)MSUShadowView.h 中内容

            #import <UIKit>

            @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 xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed xss=removed>

1)输入框点击方法
    #pragma mark -
- (void)textFieldDidChange:(UITextField *)textField{
    if (textField == self.pwdTF) {
        if (textField.text.length > 0 && textField.text.length < 7 xss=removed xss=removed xss=removed xss=removed xss=removed> 0 && textField.text.length &lt; 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];
    }
收藏
0
sina weixin mail 回到顶部