如今的 app 大部分会接入三方支付,同时也有一些 app 会选择一种充值模式,让用户把本身的资金存入 app 内,这样在用户须要在 app 中购买商品时,就可以用本身的钱包进行支付,在这里就会涉及到支付密码弹窗的实现,下面说一下对一个简单的密码支付弹窗实现过程。数组
首先想到输入框就避不开 UITextfield
,因此咱们的思路从 UITextfield
开始发散。bash
首先须要建立一个密码输入框,目的是能够弹起键盘,而且输入内容。app
若是以 UItextfield
为基础,将输入内容设置为密文输入显示的话,展现出来的效果是会把内容挤在一块儿,因此须要监听输入框的输入内容,而后经过输入的内容进行相应判断,而后咱们绘制好对应的小黑点,将其在"输入框"中显示。同时除了小黑点以外,咱们还须要绘制对应的线框。ui
UIView *inputView = [[UIView alloc]init];
inputView.backgroundColor = White_Color;
inputView.layer.borderWidth = 1;
inputView.layer.borderColor = UIColorFromRGB(0xb2b2b2).CGColor;
[self.alertWhiteView addSubview:inputView];
[inputView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.moneyLabel.mas_bottom).offset(19);
make.centerX.equalTo(self.alertWhiteView);
make.width.offset(squreWidth*6);
make.height.offset(squreWidth);
make.bottom.offset(-19);
}];
复制代码
for (int i = 1; i<7; i++) {
// 黑色点
UIView *spotView = [[UIView alloc]initWithFrame:CGRectMake((i-1)*squreWidth+(squreWidth-dotWidth)/2, (squreWidth-dotWidth)/2, dotWidth, dotWidth)];
spotView.backgroundColor = UIColorFromRGB(0x262122);
// 切圆
spotView.clipsToBounds = YES;
spotView.layer.cornerRadius = dotWidth/2;
// 隐藏,输入时再显示
spotView.hidden = YES;
[inputView addSubview:spotView];
// 把小黑点按照顺序依次加入数组中
[_dotArray addObject:spotView];
if (i!=6) {
// 分割线
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(i*squreWidth, 0, 1, squreWidth)];
lineView.backgroundColor = UIColorFromRGB(0xb2b2b2);
[inputView addSubview:lineView];
}
}
复制代码
UITextfield
// 密码输入框
[self.passWordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.moneyLabel.mas_bottom).offset(19);
make.centerX.equalTo(self.alertWhiteView);
make.width.offset(squreWidth*6);
make.height.offset(squreWidth);
make.bottom.offset(-19);
}];
复制代码
懒加载方式:spa
// 懒加载方式
- (UITextField *)passWordTextField{
if (!_passWordTextField) {
_passWordTextField = [[UITextField alloc]init];
// 设置为纯数字键盘
_passWordTextField.keyboardType = UIKeyboardTypeNumberPad;
[self.alertWhiteView addSubview:_passWordTextField];
// 默认隐藏
_passWordTextField.hidden = YES;
// 添加输入监听
[_passWordTextField addTarget:self action:@selector(textFieldDidChange:)forControlEvents:UIControlEventEditingChanged];
}
return _passWordTextField;
}
复制代码
// 键盘内容监听
- (void)textFieldDidChange:(UITextField *)textField{
if (textField.text.length == 6) {
// [MBProgressHUD showError:@"密码错误,请从新尝试"];
if (self.completeBlock) {
self.completeBlock(textField.text);
[textField resignFirstResponder];
textField.text = @"";
}
}
if (textField.text.length == 7) {
textField.text = [textField.text substringToIndex:1];
for (int j = 0; j<_dotArray.count; j++) {
UIView *view = _dotArray[j];
view.hidden = YES;
}
}
if (textField.text.length>0&&textField.text.length<7) {
for (int j = 0; j<_dotArray.count; j++) {
UIView *view = _dotArray[j];
if (j<textField.text.length) {
view.hidden = NO;
}else{
view.hidden = YES;
}
}
}else{
for (int j = 0; j<_dotArray.count; j++) {
UIView *view = _dotArray[j];
view.hidden = YES;
}
}
}
复制代码
passWordTextField
的内容,并添加到自定义的"密码输入框"中passWordTextField
变成第一响应者,弹起键盘在这里省略了不少步骤,只拿最关键的过程来描述,好比其余相关视图的建立,弹起的相关逻辑,以及输入完毕的回调处理,在这里就不一一赘述了3d