.h文件
#import <UIKit/UIKit.h>
@protocol payPwsViewDelegate <NSObject>
- (void)payPwsViewDelegate;
@end
@interface PayPwsView : UIView{
}
@property(nonatomic,strong)id <payPwsViewDelegate> delegate;
@end
.m文件
//
// PayPwsView.m
// GWhome
//
// Created by 陈万里 on 15/11/27.
// Copyright © 2015年 chenwanli. All rights reserved.
//
#import "PayPwsView.h"
#define RGB_A(R,G,B,A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
// 获取屏幕高度
#define kDeviceHeight [UIScreen mainScreen].bounds.size.height
// 获取屏幕宽度
#define kDeviceWidth [UIScreen mainScreen].bounds.size.width
#define FONTNAME(font,name) [UIFont fontWithName:name size:font];
@implementation PayPwsView
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = RGB_A(0, 0, 0, 0.5);
UIView *bgView =[[UIView alloc]initWithFrame:CGRectMake((kDeviceWidth - 280) / 2, kDeviceHeight - 400, 280, 150)];
bgView.layer.cornerRadius = 5;
bgView.layer.masksToBounds = YES;
bgView.backgroundColor = [UIColor whiteColor];
[self addSubview:bgView];
UILabel *pwsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, 40)];
pwsLabel.textAlignment = 1;
pwsLabel.font = FONTNAME(18, @"Helvetica-Bold");
pwsLabel.text = @"请输入支付密码";
[bgView addSubview:pwsLabel];
// 分割线
UIImageView *separatorImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 39, bgView.frame.size.width, 1)];
separatorImg.backgroundColor = [UIColor redColor];
[bgView addSubview:separatorImg];
float initialX = (bgView.frame.size.width - 40 * 6) / 2;
for (int i = 0; i < 6; i++) {
UITextField *pwsField = [[UITextField alloc]initWithFrame:CGRectMake(initialX + i * 40, 75, 40, 40)];
pwsField.textAlignment = 1;
pwsField.tag = i + 100;
pwsField.keyboardType = UIKeyboardTypeNumberPad;
pwsField.layer.borderWidth = 0.5;
pwsField.layer.borderColor = [UIColor blackColor].CGColor;
pwsField.secureTextEntry = YES;
pwsField.userInteractionEnabled = NO;
[bgView addSubview:pwsField];
}
UIButton *pwsBut = [UIButton buttonWithType:UIButtonTypeCustom];
pwsBut.frame = CGRectMake(initialX, 100, 35 * 6, 35);
[pwsBut addTarget:self action:@selector(pwsBut:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:pwsBut];
UITextField *bigPwsField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
bigPwsField.hidden = YES;
bigPwsField.textAlignment = 1;
bigPwsField.tag = 1000;
bigPwsField.keyboardType = UIKeyboardTypeNumberPad;
[bigPwsField addTarget:self action:@selector(pwsField:) forControlEvents:UIControlEventEditingChanged];
[bgView addSubview:bigPwsField];
}
return self;
}
- (void)pwsBut:(UIButton *)sender{
UITextField *pwsField = (UITextField *)[self viewWithTag:1000];
if (sender == nil) {
pwsField.text = @"";
}
[self distributionPws];
[pwsField becomeFirstResponder];
}
- (void)distributionPws{
UITextField *bigPwsField = (UITextField *)[self viewWithTag:1000];
for (int i = 0; i < 6; i++) {
if (i < bigPwsField.text.length) {
UITextField *pwsField = (UITextField *)[self viewWithTag:100 + i];
pwsField.text = [bigPwsField.text substringWithRange:NSMakeRange(i,1)];
}else{
UITextField *pwsField = (UITextField *)[self viewWithTag:100 + i];
pwsField.text = @"";
}
}
}
- (void)pwsField:(UITextField *)sender{
if (sender.text.length < 7) {
[self distributionPws];
if (sender.text.length == 6) {
[self kenEnd];
[self.delegate payPwsViewDelegate];
}
}else{
sender.text = [sender.text substringWithRange:NSMakeRange(0,sender.text.length - 1)];
}
}
- (void)setFrame:(CGRect)frame{
[super setFrame:frame];
if (frame.origin.y < 1) {
[self pwsBut:nil];
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self kenEnd];
}
- (void)kenEnd{
[self.superview endEditing:YES];
[UIView animateWithDuration:0.5 animations:^{
self.frame = CGRectMake(0,kDeviceHeight,kDeviceWidth, kDeviceHeight);
}];
}
@end