iOS密码框的实现方式

说一下密码加密的实现方式
 
效果图:
 
 
CC05CAD0 C90F 46B8 9F5C 63B3FA27BB55
 
 
 
实现方式:
 
主要说一下密码框的实现,这个密码框中间的四个数字其实是4个  UITextField ,然后通过键盘删除键 和TextFiled 的协议 shouldChangeCharactersInRange.来判断输入的位置,代码如下;
 
 
 
直接上代码:
 
//
//  IDSGameRoomSecretView.h
//
//  Created by levi.duan on 2017/7/18.
//  Copyright © 2017 Near. All rights reserved.
//

#import <UIKit/UIKit.h>
#import
"BaseViewController.h"

typedef void (^handleInputPasswordBlock)( NSString *password);

@interface IDSGameRoomSecretView : UIView

- (
instancetype )initWithPasswordCallBack:( handleInputPasswordBlock )passwordCallback;

/**
 
弹出说明视图
 */

- (
void )showInputSecretView;


@end
 
 
 
 
 
 
//
//  IDSGameRoomSecretView.m
//  Near
//
//  Created by levi.duan on 2017/7/18.
//  Copyright © 2017 Near. All rights reserved.
//

#import "IDSGameRoomSecretView.h"
#import
"PZXVerificationTextField.h"


@interface IDSGameRoomSecretView ()< UIGestureRecognizerDelegate , UITextFieldDelegate , PZXTextFieldDelegate >

@property ( nonatomic , strong ) UILabel *titleLabel;
@property ( nonatomic , strong ) UILabel *subtitleLabel;
@property ( nonatomic , strong ) UITextField *secretTextField;
@property ( nonatomic , strong ) UIButton *gameRoomBtn;
@property ( nonatomic , strong ) UIView *secretRoomView;
@property ( nonatomic , strong ) NSMutableArray *textFieldArray;
@property ( nonatomic , copy ) handleInputPasswordBlock onHandlePasswordCallBack;



@end

@implementation IDSGameRoomSecretView

- (
instancetype )initWithPasswordCallBack:( handleInputPasswordBlock )passwordCallback
{
   
if ( self = [ super init ]) {
       
self . onHandlePasswordCallBack = passwordCallback;
    }
   
   
return self ;
}

- (
void )showInputSecretView
{

   
self . backgroundColor = [ UIColor colorWithRed : 0 green : 0 blue : 0 alpha : 0.7 ];

    [[
AppDelegate mainWindow ] addSubview : self ];
    [
self . inputView becomeFirstResponder ];
   
   
self . frame = CGRectMake ( 0 , 0 , SCREEN_WIDTH , SCREEN_HEIGHT );
   
   
UITapGestureRecognizer *selfRecognizer = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (removeView)];
   
self . userInteractionEnabled = YES ;
    [
self addGestureRecognizer :selfRecognizer];
    selfRecognizer.
delegate = self ;
   
   
self . secretRoomView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 510 / 2 , 380 / 2 )];
   
self . secretRoomView . centerY = SCREEN_HEIGHT / 2 ;
   
self . secretRoomView . centerX = SCREEN_WIDTH / 2 ;
   
self . secretRoomView . backgroundColor = [ UIColor whiteColor ];
   
   
self . secretRoomView . centerY = SCREEN_HEIGHT / 2 - 50 ;
 
   

   
_titleLabel = [[ UILabel alloc ] initWithFrame : CGRectMake ( 0 , 50 / 2 , 0 , 0 )];
   
_titleLabel . text = @" 房间已加锁 " ;
   
_titleLabel . textColor = NF_Color_C3 ;
   
_titleLabel . font = [ UIFont systemFontOfSize : Near_Final_Font_T6 ];
    [
_titleLabel sizeToFit ];
   
_titleLabel . centerX = self . secretRoomView . frame . size . width / 2 ;
    [
self . secretRoomView addSubview : _titleLabel ];
   
   
_subtitleLabel = [[ UILabel alloc ] initWithFrame : CGRectMake ( 0 , CGRectGetMaxY ( self . titleLabel . frame )+ 10 , 0 , 0 )];
   
_subtitleLabel . text = @" 输入房间密码 " ;
   
_subtitleLabel . textColor = NF_Color_C10 ;
   
_subtitleLabel . font = [ UIFont systemFontOfSize : Near_Final_Font_T9 ];
    [
_subtitleLabel sizeToFit ];
   
_subtitleLabel . centerX = self . secretRoomView . frame . size . width / 2 ;
    [
self . secretRoomView addSubview : _subtitleLabel ];
   
   
self . textFieldArray = [ NSMutableArray array ];
   
NSArray *views = [ self subviews ];
   
for ( UITextField *tf in views) {
        [tf
removeFromSuperview ];
    }
   
   
for ( int i= 0 ;i< 4 ;++i) {
       
PZXVerificationTextField *tf = [[ PZXVerificationTextField alloc ] initWithFrame : CGRectMake ( 70 / 2 +i* 70 / 2 + 15 *i, CGRectGetMaxY ( self . subtitleLabel . frame )+ 15 , 70 / 2 , 70 / 2 )];
        [tf
setFont :[ UIFont systemFontOfSize : Near_Final_Font_T5 ]];
        [tf
setTextColor : NF_Color_C4 ];
        tf.
backgroundColor = [ UIColor clearColor ];
        tf.
layer . borderWidth = 0.5 ;
        tf.
layer . borderColor = NF_Color_C9 . CGColor ;
        tf.
layer . cornerRadius = 5.f ;
        tf.
layer . masksToBounds = YES ;
        tf.
tintColor =[ UIColor clearColor ];
        tf.
tag = 100 +i;
        tf.
keyboardType = UIKeyboardTypeNumberPad ;
        tf.
textAlignment = NSTextAlignmentCenter ;
        tf.
delegate = self ;
        tf.
pzx_delegate = self ;
        [
self . secretRoomView addSubview :tf];
        [
self . textFieldArray cl_addObject :tf];
        [tf
becomeFirstResponder ];
    }
   
   
self . gameRoomBtn = [[ UIButton alloc ] initWithFrame : CGRectMake ( 0 , 270 / 2 , 228 / 2 , 68 / 2 )];
   
self . gameRoomBtn . centerX = self . secretRoomView . frame . size . width / 2 ;
   
    [
self . gameRoomBtn setTitle : @" 确定 " forState : UIControlStateNormal ];
    [
self . gameRoomBtn . titleLabel setFont :[ UIFont systemFontOfSize : Near_Final_Font_T5 ]];
    [
self . gameRoomBtn . titleLabel setTextColor : NF_Color_C1 ];
   
self . gameRoomBtn . backgroundColor = NF_Color_C27 ;
    [
self . gameRoomBtn addTarget : self action : @selector (interRoomVoid) forControlEvents : UIControlEventTouchUpInside ];
   
self . gameRoomBtn . layer . cornerRadius = 5.0f ;
   
self . gameRoomBtn . layer . masksToBounds = YES ;
   
    [
self . secretRoomView addSubview : self . gameRoomBtn ];
   
    [
self addSubview : self . secretRoomView ];
   
   
self . secretRoomView . layer . cornerRadius = 10.f ;
   
self . secretRoomView . layer . masksToBounds = YES ;
   
   
}

#pragma mark - Public Methods

/*
- (void)showDalog
{
    [[AppDelegate mainWindow] addSubview:self];
    [self.inputView becomeFirstResponder];
}
 */


- (
void )removeView
{
    [
self removeFromSuperview ];
}

-(
BOOL )textField:( UITextField *)textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString *)string{
   
    textField.
text = string;
   
   
if (textField. text . length > 0 ) { // 防止退格第一个的时候往后跳一格
       
       
if (textField. tag <  [[ _textFieldArray lastObject ] tag ]) {
           
           
UITextField *newTF =  ( UITextField *)[ self viewWithTag :textField. tag + 1 ];
           
            [newTF
becomeFirstResponder ];
        }
    }
   
return NO ;
}

// 点击退格键的代理
#pragma mark - PZXTextFieldDelegate
-( void )PZXTextFieldDeleteBackward:( PZXVerificationTextField *)textField{
   
   
if (textField. tag > [[ _textFieldArray firstObject ] tag ]) {
       
       
UITextField *newTF =  ( UITextField *)[ self viewWithTag :textField. tag - 1 ];
        newTF.
text = @"" ;
        [newTF
becomeFirstResponder ];
    }
   
   
}

- (
void )interRoomVoid
{
    [
self getVertificationCode ];
}

-(
void )getVertificationCode{ // 获取验证码方法
   
   
NSString *str = [ NSString string ];
   
   
for ( int i = 0 ; i< _textFieldArray . count ; i++) {
        str = [str
stringByAppendingString :[ NSString stringWithFormat : @"%@" ,( UITextField *)[ _textFieldArray [i] text ]]];
    }
   
if ( self . onHandlePasswordCallBack ) {
       
self . onHandlePasswordCallBack (str);
    }
    [
self removeView ];
}


-(
void )touchesBegan:( NSSet < UITouch *> *)touches withEvent:( UIEvent *)event{
   
   
for ( UITextField *tf in self . textFieldArray ) {
       
        [tf
resignFirstResponder ];
    }
}



- (
BOOL )gestureRecognizer:( UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:( UITouch *)touch
{
   
if ([touch. view isDescendantOfView : self . secretRoomView ]) {
       
return NO ;
    }
   
return YES ;
}


@end
 
//
//  PZXVerificationTextField.h
//  Near
//
//  Created by levi.duan on 2017/7/19.
//  Copyright © 2017 Near. All rights reserved.
//

#import <UIKit/UIKit.h>

@class PZXVerificationTextField ; // class 一下不然代理里面无法识别

@protocol PZXTextFieldDelegate < NSObject >

- (
void )PZXTextFieldDeleteBackward:( PZXVerificationTextField *)textField;


@end

@interface PZXVerificationTextField : UITextField

@property ( nonatomic , assign ) id < PZXTextFieldDelegate > pzx_delegate;

@end
 
 
 
//
//  PZXVerificationTextField.m
//  Near
//
//  Created by levi.duan on 2017/7/19.
//  Copyright © 2017 Near. All rights reserved.
//

#import "PZXVerificationTextField.h"

@implementation PZXVerificationTextField

-(
void )deleteBackward{
    [
super deleteBackward ];
   
if ([ self . pzx_delegate respondsToSelector : @selector (PZXTextFieldDeleteBackward:)]) {
       
        [
self . pzx_delegate PZXTextFieldDeleteBackward : self ];
    }
   
   
}

@end
 

转载于:https://www.cnblogs.com/firstrate/p/7208530.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值