由于将确定按钮去掉了,所以需要重新修改下代码,当输入第四个数字时,自动进入房间。
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;
tf.tintColor =[UIColorclearColor];
{
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;
}
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];
}
- (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;
}
-(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];
}
}
#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];
}
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];
}
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;
}
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;
}
}
{
[selfstopQueryTimer];
if (nil == _queryNoticeTimer) {
_queryNoticeTimer = [NSTimerscheduledTimerWithTimeInterval:sIntervalTime
target:self
selector:@selector(getVertificationCode)
userInfo:nilrepeats:NO];
}
}
- (void)stopQueryTimer
{
if (self.queryNoticeTimer) {
[self.queryNoticeTimerinvalidate];
_queryNoticeTimer = nil;
}
}