// // ViewController.m // 设置密码 #import "ViewController.h" #import "PasswordView.h" #define Color_RGB(a,b,c,d) [UIColor colorWithRed:(a)/255.0 green:(b)/255.0 blue:(c)/255.0 alpha:(d)] #define GrayColor Color_RGB(236,236,236,1)//headerView 灰 #define screenWidth [UIScreen mainScreen].bounds.size.width #define screenHeight [UIScreen mainScreen].bounds.size.height #define RGBACOLOR(a,b,c,d) Color_RGB(a,b,c,d) #define Font(a) [UIFont systemFontOfSize:a]; #define littleBlackColor Color_RGB(79,79,79,1)// 字体浅黑 #define YellowColor Color_RGB(253,131,42,1)//主题黄 #define LineGrayColor Color_RGB(210,210,210,1)//分割线 灰 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,PasswordViewDelegate> @property (nonatomic,strong) UITableView *tableView; @property (nonatomic,strong) NSMutableArray *dataArray; @property (nonatomic,strong) UIView *alphaView; @property (nonatomic,strong) NSString *checkString; @property (nonatomic,strong) NSString *updateString; @property (nonatomic) NSInteger integer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = GrayColor; self.alphaView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; self.dataArray = [NSMutableArray arrayWithObjects:@"修改提现密码",@"忘记提现密码", nil]; [self initSubview]; } -(void)initSubview{ self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight) style:UITableViewStyleGrouped]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *string = @"tradeCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:string]; } cell.textLabel.text = self.dataArray[indexPath.row]; cell.textLabel.font = Font(14); cell.textLabel.textColor = littleBlackColor; cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"personal_arrow"]]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; self.integer = 1; [self initAlphaViewAndString:@"请输入原提现密码"]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 43; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.001; } -(void)initAlphaViewAndString:(NSString *)string { self.alphaView.backgroundColor = RGBACOLOR(0, 0, 0, 0.3); self.alphaView.alpha = 1; // UIWindow *window = [[UIApplication sharedApplication].windows lastObject]; // [window addSubview:self.alphaView]; [self.view addSubview:self.alphaView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapToDismiss)]; [self.alphaView addGestureRecognizer:tap]; UIView *whiteV = [[UIView alloc]initWithFrame:CGRectMake(20, 110, screenWidth - 40, 140)]; whiteV.backgroundColor = [UIColor whiteColor]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 25, screenWidth - 80, 15)]; label.textColor = YellowColor; label.text = string; label.font = Font(12); [whiteV addSubview:label]; PasswordView *password = [[PasswordView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(label.frame) + 20, screenWidth - 80, 43)]; [whiteV addSubview:password]; password.layer.borderColor = LineGrayColor.CGColor; password.layer.borderWidth = 0.5; password.delegate = self; password.backgroundColor = [UIColor whiteColor]; [self.alphaView addSubview:whiteV]; CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.duration = 0.5; NSMutableArray *values = [NSMutableArray array]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]]; [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]]; animation.values = values; [whiteV.layer addAnimation:animation forKey:nil]; } -(void)tapToDismiss{ [UIView animateWithDuration:0.2 animations:^{ [self.alphaView endEditing:YES]; self.alphaView.alpha = 0; } completion:^(BOOL finished) { [self.alphaView removeFromSuperview]; }]; } #pragma mark PasswordView Delegate -(void)passWordDidChange:(PasswordView *)password { NSLog(@"改变%@",password.textStore); } -(void)passWordBeginInput:(PasswordView *)password { NSLog(@"开始输入%@",password.textStore); } -(void)passWordCompleteInput:(PasswordView *)password { NSLog(@"输入完成%@",password.textStore); if (password.textStore.length == 6 && self.integer == 1) { self.checkString = password.textStore; //延迟执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self checkPassword]; }); } if (password.textStore.length == 6 && self.integer == 2) { self.updateString = password.textStore; //延迟执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self updatePassword]; }); } } //验证原始密码 -(void)checkPassword { [self tapToDismiss]; NSLog(@"验证原始密码"); } //更改密码 -(void)updatePassword { [self tapToDismiss]; NSLog(@"更改密码"); } @end
// // PasswordView.h #import <UIKit/UIKit.h> @class PasswordView; @protocol PasswordViewDelegate<NSObject> -(void)passWordDidChange:(PasswordView *)password; -(void)passWordCompleteInput:(PasswordView *)password; -(void)passWordBeginInput:(PasswordView *)password; @end @interface PasswordView : UIView<UIKeyInput> @property (nonatomic) NSInteger passWordNum;//密码的位数 @property (nonatomic) CGFloat squareWidth;//密码框的大小 @property (nonatomic) CGFloat pointRadius;//黑点半径 @property (nonatomic) UIColor *pointColor;//黑点颜色 @property (nonatomic) UIColor *rectColor;//边框颜色 @property (nonatomic) id<PasswordViewDelegate> delegate;// @property (nonatomic,readonly,strong) NSMutableString *textStore;//保存密码的字符串 @end
// // PasswordView.m #import "PasswordView.h" #define Color_RGB(a,b,c,d) [UIColor colorWithRed:(a)/255.0 green:(b)/255.0 blue:(c)/255.0 alpha:(d)] #define LineGrayColor Color_RGB(210,210,210,1)//分割线 灰 #define BlackColor [UIColor blackColor] @interface PasswordView () @property (nonatomic,strong) NSMutableString *textStore; @end @implementation PasswordView static NSString * const MONEYNUMBERS = @"0123456789"; -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.textStore = [NSMutableString string]; self.squareWidth = frame.size.width/6.0; self.passWordNum = 6; self.pointRadius = 6; self.rectColor = LineGrayColor; self.pointColor = BlackColor; [self becomeFirstResponder]; } return self; } /** * 设置密码框边长 */ -(void)setSquareWidth:(CGFloat)squareWidth { _squareWidth = squareWidth; [self setNeedsDisplay]; } /** * 设置键盘类型 */ -(UIKeyboardType)keyboardType { return UIKeyboardTypeNumberPad; } /** * 设置密码位数 * */ -(void)setPassWordNum:(NSInteger)passWordNum { _passWordNum = passWordNum; [self setNeedsDisplay]; } -(BOOL)becomeFirstResponder { if ([self.delegate respondsToSelector:@selector(passWordBeginInput:)]) { [self.delegate passWordBeginInput:self]; } return [super becomeFirstResponder]; } /** * 是否成为第一响应者 * */ -(BOOL)canBecomeFirstResponder { return YES; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { if (![self isFirstResponder]) { [self becomeFirstResponder]; } } #pragma mark - UIKeyInput /** * 用于显示的文本对象是否有任何文本 */ -(BOOL)hasText { return self.textStore.length > 0; } /** *插入文本 */ -(void)insertText:(NSString *)text { if (self.textStore.length < self.passWordNum) { //判断是否有数字 NSCharacterSet *character = [[NSCharacterSet characterSetWithCharactersInString:MONEYNUMBERS] invertedSet]; NSString *filter = [[text componentsSeparatedByCharactersInSet:character] componentsJoinedByString:@""]; BOOL basicTest = [text isEqualToString:filter]; if (basicTest) { [self.textStore appendString:text]; if ([self.delegate respondsToSelector:@selector(passWordDidChange:)]) { [self.delegate passWordDidChange:self]; } if (self.textStore.length == self.passWordNum) { if ([self.delegate respondsToSelector:@selector(passWordCompleteInput:)]) { [self.delegate passWordCompleteInput:self]; } } [self setNeedsDisplay]; } } } //删除文本 -(void)deleteBackward { if (self.textStore.length > 0) { [self.textStore deleteCharactersInRange:NSMakeRange(self.textStore.length - 1, 1)]; if ([self.delegate respondsToSelector:@selector(passWordDidChange:)]) { [self.delegate passWordDidChange:self]; } } [self setNeedsDisplay]; } -(void)drawRect:(CGRect)rect { CGFloat height = rect.size.height; CGFloat width = rect.size.width; CGFloat x = (width - self.squareWidth * self.passWordNum)/2.0; CGFloat y = (height - self.squareWidth)/2.0; CGContextRef context = UIGraphicsGetCurrentContext(); // //画外框 //// // CGContextAddRect(context, CGRectMake(x, y, self.squareWidth * self.passWordNum, self.squareWidth)); CGContextSetLineWidth(context, 0.5); CGContextSetStrokeColorWithColor(context, self.rectColor.CGColor); // CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor); // //画竖条 for (int i = 1; i < self.passWordNum; i ++) { CGContextMoveToPoint(context, x + self.squareWidth * i, y); CGContextAddLineToPoint(context, x + self.squareWidth * i, y + self.squareWidth); CGContextClosePath(context); } CGContextDrawPath(context, kCGPathStroke); CGContextSetFillColorWithColor(context, self.pointColor.CGColor); //画黑点 for (int i = 1; i <= self.textStore.length; i++) { CGContextAddArc(context, x + self.squareWidth * i - self.squareWidth/2.0, y + self.squareWidth/2.0, self.pointRadius, 0, M_PI * 2, YES); CGContextDrawPath(context, kCGPathFill); } } @end