// // WPSignPasswordView.h // 网投网 // // Created by wangtouwang on 15/4/9. // Copyright (c) 2015年 wangtouwang. All rights reserved. // #import <UIKit/UIKit.h> @class WPSignPasswordView; @protocol WPSignPasswordDelegate <NSObject> //设置密码 确认成功 @required -(void)setPawSuccess:(NSString *)password; //设置第一次临时密码成功 @required -(void)setFirstPasswordSuccess:(NSString *)password; //第二次输入确认密码错误 -(void)setTwoPasswordError; //修改手势密码 请输入之前的密码 -(void)setSuccessAfterFirstPS:(NSString *)password; //进入程序后输入手势密码判断是否正确 -(void)confirmPassword:(NSString *)password; //手势密码进入修改状态(即原密码输入成功) 首次输入 -(void)updateSPFirst:(NSString *)password; //手势密码进入修改状态(即原密码输入成功) 二次输入,相当于输入密码确认 -(void)updateSPConfirm:(NSString *)password; @end #pragma mark 手势密码View @interface WPSignPasswordView : UIView //设置代理 @property(nonatomic,strong) id<WPSignPasswordDelegate> spDelegate; @end
// // WPSignPasswordView.m // 网投网 // // Created by wangtouwang on 15/4/9. // Copyright (c) 2015年 wangtouwang. All rights reserved. // #import "WPSignPasswordView.h" //屏幕的长宽 #define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height @interface WPSignPasswordView() { } @property(nonatomic,strong) NSMutableArray *stringArrays; @property(nonatomic,strong) NSMutableArray *allButtonsArray; //定义一个属性,记录当前点 @property(nonatomic,assign)CGPoint currentPoint; @end @implementation WPSignPasswordView #pragma mark 实例化收集字符串的数组 并且用懒加载 -(NSMutableArray *)getStringArrays{ if (self.stringArrays==nil) { self.stringArrays = [NSMutableArray array]; } return self.stringArrays; } #pragma mark 实例化包含所有密码按钮的数组 并且用懒加载 -(NSMutableArray *)getAllButtonsArray{ if (self.allButtonsArray==nil) { self.allButtonsArray = [NSMutableArray array]; } return self.allButtonsArray; } #pragma mark 复写初始化界面函数 initFrame -(instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { [self setup]; } return self; } #pragma mark 界面布局 -(void)setup{ //NSLog(@"初始化界面布局执行的"); for (int index=1; index<=9; index++) { //创建按钮 UIButton *numberButton = [[UIButton alloc] init]; //设置按钮的背景图片,并且设置是在何种状态下 [numberButton setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal]; [numberButton setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected]; //将按钮添加到视图中 [self addSubview:numberButton]; //将按钮存储到按钮数组中 [[self getAllButtonsArray] addObject:numberButton]; //禁止按钮点击事件 numberButton.userInteractionEnabled=NO; //设置按钮标志值 numberButton.tag=index; } } #pragma mark 看看何时触发 -(void)layoutSubviews{ //需要先调用父类的方法 [super layoutSubviews]; //设置按钮位置 for (int index=0; index<self.allButtonsArray.count; index++) { CGFloat inverst_top = KSCREEN_HEIGHT/4; inverst_top=0; UIButton *btn =(UIButton *) self.allButtonsArray[index]; //NSLog(@"%i",btn.tag); //获取行号 CGFloat row = index/3; //获取坐标号 CGFloat loc = index%3; // 按钮长跟宽 CGFloat btnHeight = 75; CGFloat btnWith=btnHeight; //获取按钮间隔距离 CGFloat inverst = (KSCREEN_WIDTH-btnHeight*3)/4; //X Y 坐标 CGFloat btnX = inverst +loc*(btnWith+inverst); CGFloat btnY = inverst + row*(btnHeight+inverst); btn.frame=CGRectMake(btnX, btnY+inverst_top-15, btnHeight, btnHeight); } } #pragma mark 复写画布布局 - (void)drawRect:(CGRect)rect { //NSLog(@"再次触发吗"); //获取上下文 CGContextRef context = UIGraphicsGetCurrentContext(); #pragma mark 填充画布颜色 //填充上下文颜色 CGContextSetFillColorWithColor(context, [[UIColor grayColor] CGColor]); //补充当前填充颜色的rect CGContextFillRect(context, rect); #pragma mark -实现画线功能 int i =0; //绘图(线段) for (UIButton *btn in self.stringArrays) { if (0==i) { //设置起点(注意连接的是中点) CGContextMoveToPoint(context, btn.center.x, btn.center.y); }else{ CGContextAddLineToPoint(context, btn.center.x, btn.center.y); } i++; } //当所有按钮的中点都连接好之后,再连接手指当前的位置 //判断数组中是否有按钮,只有有按钮的时候才绘制 if (self.stringArrays.count !=0) { //画直线 CGContextAddLineToPoint(context, self.currentPoint.x, self.currentPoint.y); } //渲染 //设置线条的宽度 CGContextSetLineWidth(context, 10); //设置图像上下文中的接接线的样式。 CGContextSetLineJoin(context, kCGLineJoinRound); //设置线条终点形状 CGContextSetLineCap(context, kCGLineCapRound); //画笔颜色设置 CGContextSetRGBStrokeColor(context, 255/255.0, 100/255.0, 0/255.0, 1); //开始绘制图片 CGContextStrokePath(context); } #pragma mark 复写 UIResponder--API 监听手指移动开始 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *uitouch = [touches anyObject]; CGPoint point = [uitouch locationInView:uitouch.view]; UIButton *targetBtn = nil; for (UIButton *btn in self.allButtonsArray ) { if (CGRectContainsPoint(btn.frame, point)) { targetBtn = btn; break; } } if (targetBtn&&targetBtn.selected!=YES) { targetBtn.selected=YES; [[self getStringArrays] addObject:targetBtn]; } } #pragma mark 复写 UIResponder--API 监听手指移动 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *uitouch = [touches anyObject]; CGPoint movepoint = [uitouch locationInView:uitouch.view]; UIButton *targetBtn = nil; for (UIButton *btn in self.allButtonsArray ) { if (CGRectContainsPoint(btn.frame, movepoint)) { targetBtn = btn; break; } } if (targetBtn && targetBtn.selected != YES) { //设置按钮的选中状态 targetBtn.selected=YES; //把按钮添加到数组中 [[self getStringArrays] addObject:targetBtn]; } //记录当前点(不在按钮的范围内) self.currentPoint=movepoint; //通知view重新绘制 [self setNeedsDisplay]; } #pragma mark 复写 UIResponder--API 监听手指离开屏幕 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ //取出用户输入的密码 创建一个可变的字符串,用来保存用户密码 取出用户输入的密码 NSMutableString *result=[NSMutableString string]; for (UIButton *btn in self.stringArrays) { [result appendFormat:@"%lu",btn.tag]; } //NSLog(@"用户输入的密码为:%@",result);` //清空连线记录 [self.stringArrays makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)]; //清空数组 [self.stringArrays removeAllObjects]; [self setNeedsDisplay]; //清空当前点 self.currentPoint=CGPointZero; //获取NSUserDefaults对象,判断该对象中是否已存在手势密码 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; BOOL flag = [userDefaults boolForKey:@"flag"]; NSInteger temp_signpaw = [userDefaults integerForKey:@"temp_signpaw"]; if (!flag) { #pragma mark -还未设置密码 //记录临时密码 if (temp_signpaw==0) { if ([self.spDelegate respondsToSelector:@selector(setFirstPasswordSuccess:)]) { [self.spDelegate setFirstPasswordSuccess:result]; } } //NSLog(@"%i",temp_signpaw); if (temp_signpaw!=0&&temp_signpaw==[result integerValue]) { //正确跳转 if ([self.spDelegate respondsToSelector:@selector(setPawSuccess:)]) { [self.spDelegate setPawSuccess:result]; } }else if(temp_signpaw!=0&&temp_signpaw!=[result integerValue]){ //错误处理 if([self.spDelegate respondsToSelector:@selector(setTwoPasswordError)]){ [self.spDelegate setTwoPasswordError]; } } }else{ #pragma mark -已设置密码了 NSInteger status = [userDefaults integerForKey:@"status"]; if (status==-1) { //调试状态 NSLog(@"调试状态"); }else{ //正常状态 //判断是从那个窗口跳转过来的 NSString *page = [userDefaults objectForKey:@"turnPage"]; if ([page isEqualToString:@"mainPage"]) { if ([self.spDelegate respondsToSelector:@selector(confirmPassword:)]) { [self.spDelegate confirmPassword:result]; } }else{ NSInteger updateStatus = [userDefaults integerForKey:@"updateStatus"]; if (updateStatus==1) { NSInteger tempUpdate_signpaw = [userDefaults integerForKey:@"tempUpdate_signpaw"]; if (tempUpdate_signpaw==0) { if ([self.spDelegate respondsToSelector:@selector(updateSPFirst:)]) { [self.spDelegate updateSPFirst:result]; } } if(tempUpdate_signpaw!=0){ if ([self.spDelegate respondsToSelector:@selector(updateSPConfirm:)]) { [self.spDelegate updateSPConfirm:result]; } } } if(updateStatus!=1){ //判断是否为已成功输入手势密码 if ([self.spDelegate respondsToSelector:@selector(setSuccessAfterFirstPS:)]) { [self.spDelegate setSuccessAfterFirstPS:result]; } } } } } } @end