iOS仿支付宝输入支付密码框

类似于下图这样的输入框,并实现其功能

实现这个页面,其中输入框部分为核心问题,仅针对输入框部分进行解读 下面代码只看其功能不看其位置信息

    topTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, wayLine.bottom, passView.width, topLabel.height*2)];
    topTextField.hidden = YES;
    topTextField.keyboardType = UIKeyboardTypePhonePad;
    [topTextField addTarget:self action:@selector(txChange:) forControlEvents:UIControlEventEditingChanged];
    [passView addSubview:topTextField];
    
    
    for (int i = 0; i < 6; i++)
    {
        UITextField *pwdLabel = [[UITextField alloc] initWithFrame:CGRectMake(20+i*(passView.width - 40)/6, wayLine.bottom + 10, (passView.width - 40)/6, (passView.width - 40)/6)];
        pwdLabel.layer.borderColor = [TncTool colorWithHexString:@"d2d2d2"].CGColor;
        pwdLabel.enabled = NO;
        pwdLabel.backgroundColor = [UIColor whiteColor];
        pwdLabel.textAlignment = NSTextAlignmentCenter;//居中
        pwdLabel.secureTextEntry = YES;//设置密码模式
        pwdLabel.layer.borderWidth = 1;
        [passView addSubview:pwdLabel];
        
        [textMuArray addObject:pwdLabel];
    }

实现topTextField添加的事件

- (void)txChange:(UITextField *)textField{
    NSString *password = textField.text;
    
    if (password.length == textMuArray.count)
    {
        [textField resignFirstResponder];//隐藏键盘
    }
    
    for (int i = 0; i < textMuArray.count; i++)
    {
        UITextField *pwdtx = [textMuArray objectAtIndex:i];
        if (i < password.length)
        {
            NSString *pwd = [password substringWithRange:NSMakeRange(i, 1)];
            pwdtx.text = pwd;
        }
        
    }
    
    if (password.length == 6)
    {
        [topTextField resignFirstResponder];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"输入的密码是" message:password delegate:nil cancelButtonTitle:@"完成" otherButtonTitles:nil, nil];
        [alert show];
    }

}

这样就解决的输入框的问题,至于具体的逻辑就要看产品狗的需求了

 

posted @ 2016-08-16 14:43  NSJELLY  阅读(2400)  评论(0编辑  收藏  举报