iOS-仿京東6位密碼支付輸入框


概述

用於安全支付的密碼支付輸入框.

詳細

開發需求中有時候我們需要用於安全支付的功能, 需要設置APP錢包的支付密碼, 頁面是仿照京東的6位輸入框的的做法, 效果如下圖:

47775A92-2454-45D6-8C69-054AFB968B94.png

看起來是有由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;

創建分割線和黑點.

#pragma mark - 懶加載
- (NSMutableArray *)dotArr {
    if (!_dotArr) {
        _dotArr = [NSMutableArray array];
    }
    return _dotArr;
}
- (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];
}

三、運行效果與文件截圖

1、運行效果:

2、文件截圖:

3.jpg

四、其他補充

我這里是做6位支付密碼的, 你同樣可以修改kDotCount密碼個數值,目前也有4位的.

 

界面性問題可以根據自己項目需求調整即可, 具體可參考代碼, 項目能夠直接運行!

注:本文著作權歸作者,由demo大師發表,拒絕轉載,轉載需要作者授權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM