首页 > 学院 > 开发设计 > 正文

(转部分修改)IOS手势密码(简单版)

2019-11-14 19:17:51
字体:
来源:转载
供稿:网友
////  Created by wangtouwang on 15/4/7.//  Copyright (c) 2015年 wangtouwang. All rights reserved.//#import <UIKit/UIKit.h>@class YYLockView;@PRotocol YYLockViewDelegate <NSObject>//自定义一个协议 //协议方法,把当前视图作为参数 -(void)LockViewDidClick:(YYLockView *)lockView andPwd:(NSString *)pwd;@end@interface YYLockView : UIView//代理@property(nonatomic,weak) IBOutlet id<YYLockViewDelegate>delegate;@end

 

////  YYLockView.m//  Created by wangtouwang on 15/4/7.//  Copyright (c) 2015年 wangtouwang. All rights reserved.//#import "YYLockView.h"// 设置获取屏幕长宽全局变量#define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width#define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height#define BUTTON_HEIGHT_MIDDLE_WIDTH_RADIO 1.22@interface YYLockView (){    UIView *newView;}@property(nonatomic,strong)NSMutableArray *buttons;@property(nonatomic,strong)NSMutableArray *includButtons;//定义一个属性,记录当前点@property(nonatomic,assign)CGPoint currentPoint;@end@implementation YYLockView#pragma mark-懒加载-(NSMutableArray *)buttons{    if (_buttons==nil) {        _buttons=[NSMutableArray array];    }    return _buttons;}#pragma mark-懒加载-(NSMutableArray *)includButtons{    if (_includButtons==nil) {        _includButtons=[NSMutableArray array];    }    return _includButtons;}-(id)initWithCoder:(NSCoder *)aDecoder{    NSLog(@"断点 B 号 ");    if (self=[super initWithCoder:aDecoder]) {        [self setup];    }    return self;}//界面搭建- (id)initWithFrame:(CGRect)frame{    NSLog(@"断点  1 号 ");    self = [super initWithFrame:frame];    if (self) {        [self setup];    }    return self;}//在界面上创建9个按钮-(void)setup{//    NSLog(@"断点 2 号 ");//    newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];//    newView.backgroundColor=[UIColor redColor];//    [self addSubview:newView];    //1.创建9个按钮    for (int i=0; i<9; i++) {        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];        //2.设置按钮的状态背景        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];        //3.把按钮添加到视图中        [self  addSubview:btn];        //将按钮添加到包含按钮数组中        [[self includButtons] addObject:btn];        //4.禁止按钮的点击事件        btn.userInteractionEnabled=NO;        //5.设置每个按钮的tag        btn.tag=i+1;    }}//4.设置按钮的frame-(void)layoutSubviews{    NSLog(@"断点顺序 3 号 ");    CGFloat inverst = KSCREEN_HEIGHT/4.5;    inverst=0;    //4.1需要先调用父类的方法    [super layoutSubviews];        for (int i=0; i<[self includButtons].count; i++) {        //4.2取出按钮        UIButton *btn=[self includButtons][i];        //4.3九宫格法计算每个按钮的frame        CGFloat row = i/3;        CGFloat loc   = i%3;//        NSLog(@"ROW=%f LOC=%f",row,loc);        CGFloat btnW=74;        CGFloat btnH=74;        CGFloat padding=(self.frame.size.width-3*btnW)/4;        CGFloat btnX=padding+(btnW+padding)*loc;        CGFloat btnY=padding*BUTTON_HEIGHT_MIDDLE_WIDTH_RADIO+(btnW+padding)*row;//        NSLog(@"BTNX-%f BTNY=%f ",btnX,btnY);        btn.frame=CGRectMake(btnX, btnY+inverst, btnW, btnH);    }}//重写drawrect:方法-(void)drawRect:(CGRect)rect{    NSLog(@"断点顺序 4 号 ");        //获取上下文    CGContextRef ctx=UIGraphicsGetCurrentContext();    //在每次绘制前,清空上下文    CGContextClearRect(ctx, rect);        //填充画布颜色    [[self renderImageWithColor:[UIColor grayColor] inSize:CGSizeMake(KSCREEN_WIDTH, KSCREEN_HEIGHT)] drawInRect:CGRectMake(0, 0, KSCREEN_WIDTH, KSCREEN_HEIGHT)];//在坐标中画出图片            //绘图(线段)    for (int i=0; i<self.buttons.count; i++) {        UIButton *btn=self.buttons[i];        if (0==i) {            //设置起点(注意连接的是中点)            //            CGContextMoveToPoint(ctx, btn.frame.origin.x, btn.frame.origin.y);            CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);        }else        {            //            CGContextAddLineToPoint(ctx, btn.frame.origin.x, btn.frame.origin.y);            CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);        }    }        //当所有按钮的中点都连接好之后,再连接手指当前的位置    //判断数组中是否有按钮,只有有按钮的时候才绘制    if (self.buttons.count !=0) {        CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y);    }        //渲染    //设置线条的属性    CGContextSetLineWidth(ctx, 10);    CGContextSetLineJoin(ctx, kCGLineJoinRound);    CGContextSetLineCap(ctx, kCGLineCapRound);    CGContextSetRGBStrokeColor(ctx, 255/255.0, 0/255.0, 0/255.0, 1);        CGContextStrokePath(ctx);           }//填充画布颜色- (UIImage *)renderImageWithColor:(UIColor *)color inSize:(CGSize)size{    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return image;}//5.监听手指的移动-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"断点 E 号 ");    CGPoint starPoint=[self getCurrentPoint:touches];    UIButton *btn=[self getCurrentBtnWithPoint:starPoint];        if (btn && btn.selected != YES) {        btn.selected=YES;        [self.buttons addObject:btn];    }    //    [self setNeedsDisplay];}-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"断点 F 号 ");    CGPoint movePoint=[self getCurrentPoint:touches];    UIButton *btn=[self getCurrentBtnWithPoint:movePoint];    //存储按钮    //已经连过的按钮,不可再连    if (btn && btn.selected != YES) {        //设置按钮的选中状态        btn.selected=YES;        //把按钮添加到数组中        [self.buttons addObject:btn];    }    //记录当前点(不在按钮的范围内)    self.currentPoint=movePoint;    //通知view重新绘制    [self setNeedsDisplay];}//手指离开的时候清除线条-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"断点 G 号 ");    //取出用户输入的密码    //创建一个可变的字符串,用来保存用户密码    NSMutableString *result=[NSMutableString string];    for (UIButton *btn in self.buttons) {        [result appendFormat:@"%lu",btn.tag];    }    NSLog(@"用户输入的密码为:%@",result);    //通知代理,告知用户输入的密码    if ([self.delegate respondsToSelector:@selector(LockViewDidClick:andPwd:)]) {        [self.delegate LockViewDidClick:self andPwd:result];    }        //重置按钮的状态    //    for (UIButton *btn in self.buttons) {    //        btn.selected=NO;    ////        [btn setSelected:NO];    //    }        //调用该方法,它就会让数组中的每一个元素都调用setSelected:方法,并给每一个元素传递一个NO参数    [self.buttons makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];    //清空数组    [self.buttons removeAllObjects];    [self setNeedsDisplay];        //清空当前点    self.currentPoint=CGPointZero;}//对功能点进行封装-(CGPoint)getCurrentPoint:(NSSet *)touches{    NSLog(@"断点 H 号 ");    UITouch *touch=[touches anyObject];    CGPoint point=[touch locationInView:touch.view];    return point;}-(UIButton *)getCurrentBtnWithPoint:(CGPoint)point{    NSLog(@"断点 J 号 ");    for (int i=0;i<_includButtons.count;i++) {        UIButton *btn =_includButtons[i];        if (CGRectContainsPoint(btn.frame, point)) {            return btn;        }    }    return Nil;}@end

 

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表