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];
    }

}

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM