1.思路,使用UIKeyInput协议和UITextInputTraits协议来实现让一个UIView子类具有输入功能:
@interface InputView : UIView<UIKeyInput,UITextInputTraits>
/**输入完毕*/
@property (nonatomic, copy) void(^inputComplete)(NSString *password);
@end
@interface InputView ()
/**保存密码的字符串*/
@property (nonatomic ,strong)NSMutableString *textStroe;
/**存放密码黑点的数组*/
@property (nonatomic, strong)NSMutableArray *dotArray;
@end
@implementation InputView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.textStroe = [NSMutableString string];
self.dotArray = [NSMutableArray array];
self.layer.cornerRadius = 4;
self.layer.masksToBounds = YES;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.borderWidth = 1;
//绘制 竖线
CGFloat itemW = self.width / 6;
for (int i = 1; i < 6; i++) {
UIView *line = [[UIView alloc]initWithFrame:CGRectMake(itemW * i, 0, 1, self.height)];
line.backgroundColor = [UIColor grayColor];
[self addSubview:line];
}
//绘制黑色的点 设置大小为20 20
for