做支付,大多時候需要都是一個六位數(shù)的密碼界面,閑來無事,就寫了個小demo.話不多說,開代碼
在密碼輸入框的.h文件中
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)#define boxWidth (SCREEN_WIDTH -70)/6 //密碼框的寬度
@class PassWordView;@protocol PassWordViewDelegate
@optional
- (void)passwordView:(PassWordView *)passwordView withPasswordString:(NSString *)password;
@end
@interface PassWordView : UIView@property (nonatomic,assign)id delegate;
- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title;
//標題
@property (nonatomic,strong)UILabel *label_title;
@property (nonatomic,strong)UITextField *textField;
//假的輸入框
@property (nonatomic,strong)UIView *viewBox1;
@property (nonatomic,strong)UIView *viewBox2;
@property (nonatomic,strong)UIView *viewBox3;
@property (nonatomic,strong)UIView *viewBox4;
@property (nonatomic,strong)UIView *viewBox5;
@property (nonatomic,strong)UIView *viewBox6;
//密碼點
@property (nonatomic,strong)UILabel *labelPoint1;
@property (nonatomic,strong)UILabel *labelPoint2;
@property (nonatomic,strong)UILabel *labelPoint3;
@property (nonatomic,strong)UILabel *labelPoint4;
@property (nonatomic,strong)UILabel *labelPoint5;
@property (nonatomic,strong)UILabel *labelPoint6;
@end
.m文件中
@implementation PassWordView
- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title{
self = [super initWithFrame:frame];
if (self) {
//標題
_label_title = [[UILabel alloc] init];
_label_title.frame = CGRectMake(0, 20, SCREEN_WIDTH, 20);
_label_title.text = title;
_label_title.textAlignment = NSTextAlignmentCenter;
_label_title.textColor = [UIColor grayColor];
[self addSubview:_label_title];
//textFiled
_textField = [[UITextField alloc] init];
_textField.frame = CGRectMake(0, 0, 0, 0);
_textField.delegate = self;
_textField.keyboardType = UIKeyboardTypeNumberPad;
[_textField addTarget:self action:@selector(textFiledDidChange:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:_textField];
//假的輸入框
_viewBox1 = [self creatFalseTextFiledWithFrame:CGRectMake(10, 60, boxWidth, boxWidth)];
[self addSubview:_viewBox1];
_viewBox2 = [self creatFalseTextFiledWithFrame:CGRectMake(20+boxWidth*1, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
[self addSubview:_viewBox2];
_viewBox3 = [self creatFalseTextFiledWithFrame:CGRectMake(30+boxWidth*2, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
[self addSubview:_viewBox3];
_viewBox4 = [self creatFalseTextFiledWithFrame:CGRectMake(40+boxWidth*3, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
[self addSubview:_viewBox4];
_viewBox5 = [self creatFalseTextFiledWithFrame:CGRectMake(50+boxWidth*4, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
[self addSubview:_viewBox5];
_viewBox6 = [self creatFalseTextFiledWithFrame:CGRectMake(60+boxWidth*5, _viewBox1.frame.origin.y, boxWidth, boxWidth)];
[self addSubview:_viewBox6];
//密碼點
_labelPoint1 = [self creatPasswordPointWithSuperView:_viewBox1];
_labelPoint2 = [self creatPasswordPointWithSuperView:_viewBox2];
_labelPoint3 = [self creatPasswordPointWithSuperView:_viewBox3];
_labelPoint4 = [self creatPasswordPointWithSuperView:_viewBox4];
_labelPoint5 = [self creatPasswordPointWithSuperView:_viewBox5];
_labelPoint6 = [self creatPasswordPointWithSuperView:_viewBox6];
}
return self;
}
//創(chuàng)建假的輸入框
- (UIView *)creatFalseTextFiledWithFrame:(CGRect)frame{
UIView *view = [[UIView alloc] initWithFrame:frame];
[view.layer setBorderWidth:1.0];
view.layer.borderColor = [[UIColor grayColor] CGColor];
return view;
}
這個非常簡單 ,因為我也是菜鳥一枚,有什么錯誤的地方還請各位大神多多指教
下面是demo連接:http://www.reibang.com/writer#/notebooks/8035700/notes/8516833
效果如下