////wpsignPassWordView.h//网投网////Created by wangtouwang on 15/4/9.//Copyright (c) 2015年 wangtouwang. All rights reserved.//#import<UIKit/UIKit.h>@classWPSignPasswordView;@PRotocolWPSignPasswordDelegate <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#pragmamark 手势密码View@interfaceWPSignPasswordView : 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"//屏幕的长宽#defineKSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#defineKSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height@interfaceWPSignPasswordView()
{
}
@property(nonatomic,strong) NSMutableArray*stringArrays;
@property(nonatomic,strong) NSMutableArray*allButtonsArray;//定义一个属性,记录当前点@property(nonatomic,assign)CGPoint currentPoint;@end@implementationWPSignPasswordView#pragmamark 实例化收集字符串的数组 并且用懒加载
-(NSMutableArray *)getStringArrays{if(self.stringArrays==nil) {
self.stringArrays=[NSMutableArray array];
}returnself.stringArrays;
}#pragmamark 实例化包含所有密码按钮的数组 并且用懒加载
-(NSMutableArray *)getAllButtonsArray{if(self.allButtonsArray==nil) {
self.allButtonsArray=[NSMutableArray array];
}returnself.allButtonsArray;
}#pragmamark 复写初始化界面函数 initFrame
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];if(self) {
[self setup];
}returnself;
}#pragmamark 界面布局
-(void)setup{//NSLog(@"初始化界面布局执行的");for(intindex=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;
}
}#pragmamark 看看何时触发
-(void)layoutSubviews{//需要先调用父类的方法[super layoutSubviews];//设置按钮位置for(intindex=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);
}
}#pragmamark 复写画布布局
- (void)drawRect:(CGRect)rect {//NSLog(@"再次触发吗");//获取上下文CGContextRef context =UIGraphicsGetCurrentContext();#pragmamark 填充画布颜色//填充上下文颜色CGContextSetFillColorWithColor(context, [[UIColor grayColor] CGColor]);//补充当前填充颜色的rectCGContextFillRect(context, rect);#pragmamark -实现画线功能inti =0;//绘图(线段)for(UIButton *btninself.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);
}#pragmamark 复写 UIResponder--API 监听手指移动开始
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch*uitouch =[touches anyObject];
CGPoint point=[uitouch locationInView:uitouch.view];
UIButton*targetBtn =nil;for(UIButton *btninself.allButtonsArray ) {if(CGRectContainsPoint(btn.frame, point)) {
targetBtn=btn;
break;
}
}if(targetBtn&&targetBtn.selected!=YES) {
targetBtn.selected=YES;
[[self getStringArrays] addObject:targetBtn];
}
}#pragmamark 复写 UIResponder--API 监听手指移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch*uitouch =[touches anyObject];
CGPoint movepoint=[uitouch locationInView:uitouch.view];
UIButton*targetBtn =nil;for(UIButton *btninself.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];
}#pragmamark 复写 UIResponder--API 监听手指离开屏幕
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{//取出用户输入的密码 创建一个可变的字符串,用来保存用户密码 取出用户输入的密码NSMutableString *result=[NSMutableStringstring];for(UIButton *btninself.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) {#pragmamark -还未设置密码//记录临时密码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];
}
}elseif(temp_signpaw!=0&&temp_signpaw!=[result integerValue]){//错误处理if([self.spDelegate respondsToSelector:@selector(setTwoPasswordError)]){
[self.spDelegate setTwoPasswordError];
}
}
}else{#pragmamark -已设置密码了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