iOS密码框实现取消确定按钮
Posted 旅途
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS密码框实现取消确定按钮相关的知识,希望对你有一定的参考价值。
由于将确定按钮去掉了,所以需要重新修改下代码,当输入第四个数字时,自动进入房间。
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
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;
}
{
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;
t
{
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;
t
以上是关于iOS密码框实现取消确定按钮的主要内容,如果未能解决你的问题,请参考以下文章