由于将确定按钮去掉了,所以需要重新修改下代码,当输入第四个数字时,自动进入房间。
 
iOS 密码框效果图:

 

 
实现方式:
 
首先声明一个block初始化方法,因为这只是个框框,并不需要处理网络请求等等,需要提供一个block给调用方,调用方利用block 去拿到密码,利用密码做一些开房间等操作。
 
利用计时器通过0.2秒来看清输入第四位密码,用户输入4位密码后自动进入下一步操作。
 
1.头文件需要定义:
 
@classIDSGameRoomSecretView;

typedefvoid(^selfhandleInputPasswordBlock)(NSString *password ,IDSGameRoomSecretView *secretView);

@interface IDSGameRoomSecretView : UIView

- (instancetype)initWithselfPasswordCallBack:(selfhandleInputPasswordBlock)passwordCallback;

/**
 *  弹出密码框视图
 */
- (void)showInputSecretView;

/**
 *  移除密码框view 通过外部通过block来移除
 */
- (void)removeView;

/**
 *  重置密码操作
 */
-(void)resetTextField;

@end

2.初始化操作:
 
 
- (instancetype)initWithselfPasswordCallBack:(selfhandleInputPasswordBlock)passwordCallback
{
    if (self = [superinit]) {
        self.onselfHandlePasswordCallBack = passwordCallback;
    }
   
    returnself;
}
.
3.建立密码框View
 
Ps:通过4个UITextField 来声明4个密码框,
 
- (void)showInputSecretView
{

    self.backgroundColor = [UIColorcolorWithRed:0green:0blue:0alpha:0.7];
    [[AppDelegatemainWindow] addSubview:self];
    [self.inputViewbecomeFirstResponder];
   
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
   
    UITapGestureRecognizer *selfRecognizer = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(removeView)];
    self.userInteractionEnabled = YES;
    [selfaddGestureRecognizer:selfRecognizer];
    selfRecognizer.delegate = self;
   
    self.secretRoomView = [[UIViewalloc] initWithFrame:CGRectMake(0, 0, 510/2, 290/2)];
    self.secretRoomView.backgroundColor = [UIColorwhiteColor];
    self.secretRoomView.centerX = SCREEN_WIDTH/2;
    self.secretRoomView.centerY = SCREEN_HEIGHT/2-50;
 
    _titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(0, 50/2, 0, 0)];
    _titleLabel.text = @"房间已加锁";
    _titleLabel.textColor = NF_Color_C3;
    _titleLabel.font = [UIFontsystemFontOfSize:Near_Final_Font_T6];
    [_titleLabelsizeToFit];
    _titleLabel.centerX = self.secretRoomView.frame.size.width/2;
    [self.secretRoomViewaddSubview:_titleLabel];
   
    _subtitleLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(self.titleLabel.frame)+10, 0, 0)];
    _subtitleLabel.text = @"输入房间密码";
    _subtitleLabel.textColor = NF_Color_C10;
    _subtitleLabel.font = [UIFontsystemFontOfSize:Near_Final_Font_T9];
    [_subtitleLabelsizeToFit];
    _subtitleLabel.centerX = self.secretRoomView.frame.size.width/2;
    [self.secretRoomViewaddSubview:_subtitleLabel];
   
    self.textFieldArray = [NSMutableArrayarray];
    NSArray *views = [selfsubviews];
    for (UITextField *tf in views) {
        [tf removeFromSuperview];
    }
   
    for (int i=0;i<4;++i) {
        PZXVerificationTextField *tf = [[PZXVerificationTextFieldalloc] initWithFrame:CGRectMake(70/2+i*70/2+15*i, CGRectGetMaxY(self.subtitleLabel.frame)+15, 70/2, 70/2)];
        [tf setFont:[UIFontsystemFontOfSize:Near_Final_Font_T5]];
        [tf setTextColor:NF_Color_C4];
        tf.backgroundColor = [UIColorclearColor];
        tf.layer.borderWidth = 0.5;
        tf.layer.borderColor = NF_Color_C9.CGColor;
        tf.layer.cornerRadius = 5.f;
        tf.layer.masksToBounds = YES;
        tf.tintColor =[UIColorclearColor];

        //苹果文档上提到过一次,tag值较小的,如0-100为苹果保留使用,而0就是保留着给自己这个view使用的
        tf.tag = 100+i;
        tf.keyboardType = UIKeyboardTypeNumberPad;
        tf.textAlignment = NSTextAlignmentCenter;
        tf.delegate = self;
        tf.pzx_delegate = self;
        [self.secretRoomViewaddSubview:tf];
        [self.textFieldArraycl_addObject:tf];
        [tf becomeFirstResponder];
    }
   
    [selfaddSubview:self.secretRoomView];
   
    self.secretRoomView.layer.cornerRadius = 10.f;
    self.secretRoomView.layer.masksToBounds = YES;
}
 
.
4. 移除View
 
//移除密码框View
- (void)removeView
{
    [selfremoveFromSuperview];
}
.
5. TextField输入字符代理方法
 
//通过TextField代理来控制不同textfield字符的添加以及删除,以及判断第4个Textfield自动调用成功block方法
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
   
    textField.text = string;
  
    if (textField.text.length > 0) {
       
        if (textField.tag<  [[_textFieldArraylastObject] tag]) {
           
            UITextField *newTF =  (UITextField *)[selfviewWithTag:textField.tag+1];
           
            [newTF becomeFirstResponder];
        }
    }
   
    for (UITextField *tf inself.textFieldArray) {
        if([tf.textisEqualToString:@""]) {
            returnNO;
        }
    }

    if (![_queryNoticeTimerisValid]) {
        [selfstartQueryTimer];
    }
    returnNO;
}

.
6. TextField输入删除键删除字符代理
//点击退格键的代理
#pragma mark - PZXTextFieldDelegate
-(void)PZXTextFieldDeleteBackward:(PZXVerificationTextField *)textField{
   
    if (textField.tag > [[_textFieldArrayfirstObject] tag]) {
       
        UITextField *newTF =  (UITextField *)[selfviewWithTag:textField.tag-1];
        newTF.text = @"";
        [newTF becomeFirstResponder];
    }  
}
.
7. 重置密码
-(void)resetTextField{
   
    for (UITextField *tf inself.textFieldArray) {
        tf.text = @"";
        [tf resignFirstResponder];
    }
    [[_textFieldArrayfirstObject] becomeFirstResponder];
}
.
8.获取密码,进入block操作
 
-(void)getVertificationCode{ //获取密码方法
   
    NSString *str = [NSStringstring];
   
    for (int i = 0; i<_textFieldArray.count; i++) {
        str = [str stringByAppendingString:[NSStringstringWithFormat:@"%@",(UITextField *)[_textFieldArray[i] text]]];
    }
    if (self.onHandlePasswordCallBack) {
        self.onHandlePasswordCallBack(str);
    }
    if (self.onselfHandlePasswordCallBack) {
        self.onselfHandlePasswordCallBack(str,self);
    }
    [selfstopQueryTimer];
}
.
9.手势操作
 
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   
    for (UITextField *tf inself.textFieldArray) {
       
        [tf resignFirstResponder];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.viewisDescendantOfView:self.secretRoomView]) {
        returnNO;
    }
    returnYES;
}

.
10.计时器操作
 
Ps: 计时器的作用是防止密码输入到第四个字母时,用户来不及看清第四个字母,就直接进入房间操作,所以利用计时器,0.2秒才会进入下一步操作,让用户在0.2秒内看到输入第4位密码。
 
- (void)startQueryTimer
{
    [selfstopQueryTimer];
   
    if (nil == _queryNoticeTimer) {
        _queryNoticeTimer = [NSTimerscheduledTimerWithTimeInterval:sIntervalTime
                                                             target:self
                                                           selector:@selector(getVertificationCode)
                                                           userInfo:nilrepeats:NO];
    }
}

- (void)stopQueryTimer
{
    if (self.queryNoticeTimer) {
       
        [self.queryNoticeTimerinvalidate];
        _queryNoticeTimer = nil;
    }
}

 

最新文章

  1. MyBatis:统计数量
  2. Redis中Value使用hash类型的效率是普通String的两倍
  3. C++中const 的各种用法
  4. Qt 程序运行图标
  5. poj 2239 Selecting Courses (二分匹配)
  6. layout_weight的使用说明
  7. 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
  8. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
  9. 统计计算与R语言的资料汇总(截止2016年12月)
  10. 【转载】Java的四种引用
  11. 关于iOS元旦http,https的规定,官方论坛回应
  12. c# 创建,加载,修改XML文档
  13. 实验六 MapReduce实验:二次排序
  14. 什么是Unicode
  15. SQL-7查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t (group 与count)
  16. 【C++程序员学 python】python split and join 分割与合并
  17. Unicode编码:保存中文cookie
  18. linux的性能优化
  19. LeetCode: Rotate List 解题报告
  20. HTML(form标签)、CSS

热门文章

  1. Delphi 的内存操作函数(1): 给字符指针分配内存( 给字符指针(PChar、PWideChar、PAnsiChar)分配内存最佳的选择是StrAlloc。分配内存的时候会对字符串进行初始化)
  2. 11991 - Easy Problem from Rujia Liu?(的基础数据结构)
  3. python下载图片(3)
  4. 如何导入以前的qq聊天记录
  5. 狄利克雷过程(Dirichlet Process)
  6. Android面HTTP协议发送get要求
  7. WPF字体图标——IconFont
  8. if-then和if-then-else声明
  9. Matlab Tricks(二十三)—— 保存图像到 pdf
  10. [ 转]Node.js模块 require和 exports