美文网首页
iOS 手势密码加锁

iOS 手势密码加锁

作者: SunshineBrother | 来源:发表于2016-11-03 13:55 被阅读1923次

没有什么废话,直接上代码,需要的直接用。可以参考上一篇文章画图

#import "MYLineViewController.h"
#import "MyLine.h"
@interface MYLineViewController ()

@end

@implementation MYLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    MyLine *myline = [[MyLine alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    myline.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: myline];
    
    [myline chuanZhi:^(NSString *str) {
        NSLog(@"%@",str);
    }];
}

在View界面

#import <UIKit/UIKit.h>
//一会要传值的类型
typedef void(^NewBlock)(NSString*);

@interface MyLine : UIView

//声明block的属性
@property (nonatomic) NewBlock MyBlock;

//声明block方法
- (void)chuanZhi:(NewBlock)block;

@end

#import "MyLine.h"
#define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MyLine ()
//记录选中按钮
@property (nonatomic)NSMutableArray *selectArray;
//记录当前手势所在点,用来划线
@property (nonatomic)CGPoint currentPoint;

@end

@implementation MyLine
//通过storyboard 或者 xib 文件创建的时候会调用这个方法
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if ([super initWithCoder:aDecoder])
    {
        _selectArray = [[NSMutableArray alloc]init];
        [self createButton];

    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        _selectArray = [[NSMutableArray alloc]init];
        [self createButton];

    }
    return self;
}


//绘图
- (void)drawRect:(CGRect)rect {
    
    //获取绘图需要的上下文,他是专门用来保存绘画期间的数据的
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    
    if (self.selectArray.count == 0) {
        return;
    }
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    //设置填充颜色
    [[UIColor blueColor] setFill];
    
    //设置描边颜色
    [[UIColor redColor] setStroke];
    
    //设置线宽
    CGContextSetLineWidth(contextRef, 10);
    //线的样式
    path.lineJoinStyle= kCGLineCapButt;
    //设置颜色
    [[UIColor colorWithRed:56/255.0 green:210/255.0 blue:254/255.0 alpha:0.5] set];
    //便利按钮  添加连线
    for (int i = 0; i < self.selectArray.count; i++) {
        UIButton *button = self.selectArray[i];
        if (i == 0) {
            //设置起点
            [path moveToPoint:button.center];
        }else{
            //添加连线
            [path addLineToPoint:button.center];
        }
    }
    
    //如果按钮不在当前点,就把当前触摸点作为当前点,可以使线随着拖动位置变化而变化
    [path addLineToPoint:_currentPoint];
    //把路径添加到上下文
    CGContextAddPath(contextRef, path.CGPath);
    //渲染
    CGContextStrokePath(contextRef);
}

//创建button
- (void)createButton{
    //左侧距离
    float leftW = (SCREEN_WIDTH - 250)/2;
    float heightW = 100;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            if (i == 0) {
                button.tag = 100+j;
                button.frame = CGRectMake(leftW+100*j, heightW, 50, 50);
            }else if (i == 1){
                button.tag = 103+j;
                button.frame = CGRectMake(leftW+100*j, heightW+100, 50, 50);
            }else if (i == 2){
                button.tag = 106+j;
                button.frame = CGRectMake(leftW+100*j, heightW+200, 50, 50);
            }
            [button setBackgroundImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forState:UIControlStateSelected];
            button.userInteractionEnabled = NO;
            
            [self addSubview:button];
        }
    }
    
    
}

 //根据触摸点找到对应的button
- (UIButton*)buttonWithPoint:(CGPoint)point{
    for (UIButton *button in self.subviews) {
        //注意这个:CGRectContainsPoint(btn.frame, point) 如果point这个点包含在btn的frame范围内
        if (CGRectContainsPoint(button.frame, point))
        {
            return button;
        }
    }
    return nil;
}


//开始触摸
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //拿到触摸点
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    //根据触摸点找到对应的button
    UIButton *button = [self buttonWithPoint:point];
    
    if (button && (button.selected == NO)) {
        button.selected = YES;
        //把这个button添加到数组中
        [self.selectArray addObject:button];
    }
    
}

//触摸移动过程中
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //拿到触摸点
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    
    //根据触摸点找到对应的button
    UIButton *button = [self buttonWithPoint:point];
    
    if (button && (button.selected == NO)) {
        button.selected = YES;
        //把这个button添加到数组中
        [self.selectArray addObject:button];
    }else{
        _currentPoint = point;
    }
    
    //调用绘图方法
    [self setNeedsDisplay];
}

//触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    for (UIButton *button in self.subviews) {
        button.selected = NO;
    }

    NSMutableString *path = [[NSMutableString alloc]init];
    for (UIButton *button in _selectArray) {
        [path appendFormat:@"%ld",button.tag - 100];
    }
    if (self.MyBlock) {
        self.MyBlock(path);
    }
    [_selectArray removeAllObjects];
    [self setNeedsDisplay];
   
}
 
- (void)chuanZhi:(NewBlock)block{
    self.MyBlock = block;
}











@end

相关文章

  • iOS 手势密码加锁

    没有什么废话,直接上代码,需要的直接用。可以参考上一篇文章画图 在View界面

  • 使用CoreGraphics绘图

    由于近期在做一个手势密码相关的项目,绘制手势密码用到CoreGraphics框架。 兴趣驱动下,我查阅了一下iOS...

  • iOS 手势密码

    源码参考:链接密码:37gm 源码可实现设置手势密码、登陆验证手势密码、修改手势密码 另外添加了钥匙串本地保存手势...

  • iOS 手势密码

    git地址:https://github.com/LYSBuildCode/LYSGPasswordView.gi...

  • 手势密码-iOS

    手势密码实现效果:

  • ios手势密码Swift4.0

    ios手势密码Swift4.0 之前做的P2P项目用到了手势密码的功能,由于着急上线,所以在网上扒了一个,但是为了...

  • iOS手势密码开发

    前序 最近公司APP要做改版,其中要使用手势密码锁,所以提前写了一个小Demo练练手,该Demo参考了DBGues...

  • 登录注册知识点总集二——手势密码

    一、登录注册知识总集 二、手势密码知识点汇总 手势密码一般都是辅助密码,因为相对比指纹密码,手势密码的效率较低;相...

  • iOS手势解锁,手势密码,指纹验证

    Demo地址 1.手势密码 API 使用方法 2.指纹验证 关于指纹验证,之前一篇文章已经写过,这里简单封装了下 ...

  • iOS手势密码实现机制

    基于手势密码的安全性与交互性比较好,一直以来都是手机APP的标配。在接手公司项目的时候,项目已经实现了手势密码。但...

网友评论

      本文标题:iOS 手势密码加锁

      本文链接:https://www.haomeiwen.com/subject/spnbuttx.html