1、绘制手势锁

  • 具体实现代码见 GitHub 源码 QExtension

  • QTouchLockView.h

    	@interface QTouchLockView : UIView
    
    	/// 提示信息框
    @property (nonatomic, strong) UILabel *alertLabel; /**
    * 创建手势锁视图控件,获取滑动手势结果
    *
    * @param frame 手势锁视图控件的位置尺寸
    * @param result 滑动手势结果,YES 成功,NO 失败
    *
    * @return 手势锁视图控件
    */
    + (instancetype)q_touchLockViewWithFrame:(CGRect)frame
    pathResult:(void (^)(BOOL isSucceed, NSString *result))result; @end
  • QTouchLockView.m

    	#import "NSString+Hash.h"
    
    	@interface QTouchLockView ()
    
    	/// 存放选中的按钮
    @property (nonatomic, strong) NSMutableArray *selectedArray; /// 当前被选中的按钮
    @property (nonatomic, assign) CGPoint currentPoint; /// 滑动手势结果
    @property (nonatomic, copy) void (^resultBlock)(BOOL, NSString *); @end @implementation QTouchLockView /// 创建手势锁界面,获取滑动结果
    + (instancetype)q_touchLockViewWithFrame:(CGRect)frame
    pathResult:(void (^)(BOOL isSucceed, NSString *result))result { QTouchLockView *touchLockView = [[self alloc] init]; CGRect tmpFrame = frame;
    tmpFrame.size.height = frame.size.width; touchLockView.frame = tmpFrame;
    touchLockView.resultBlock = result; return touchLockView;
    } /// 初始化界面
    - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor whiteColor]; // 添加提示信息框
    self.alertLabel = [[UILabel alloc] init];
    self.alertLabel.textAlignment = NSTextAlignmentCenter;
    self.alertLabel.textColor = [UIColor redColor];
    self.alertLabel.backgroundColor = [UIColor clearColor];
    self.alertLabel.numberOfLines = 1;
    self.alertLabel.adjustsFontSizeToFitWidth = YES;
    [self addSubview:self.alertLabel]; // 添加按钮
    for (int i = 0; i < 9; i++){ UIButton *btn = [[UIButton alloc] init]; NSString *bundlePath = [[[NSBundle mainBundle] resourcePath]
    stringByAppendingPathComponent:@"QTouchLockView.bundle"]; UIImage *normalImage = [UIImage imageWithContentsOfFile:
    [bundlePath stringByAppendingPathComponent:@"gesture_node_normal"]];
    UIImage *selectedImage = [UIImage imageWithContentsOfFile:
    [bundlePath stringByAppendingPathComponent:@"gesture_node_selected"]];
    UIImage *highlightedImage = [UIImage imageWithContentsOfFile:
    [bundlePath stringByAppendingPathComponent:@"gesture_node_highlighted"]]; [btn setBackgroundImage:normalImage forState:UIControlStateNormal];
    [btn setBackgroundImage:selectedImage forState:UIControlStateSelected];
    [btn setBackgroundImage:highlightedImage forState:UIControlStateHighlighted]; // 设置 tag 值,设置按钮对应的密码值
    btn.tag = i + 1; // 关闭按钮的交互,响应触摸事件
    btn.userInteractionEnabled = NO; [self addSubview:btn];
    }
    }
    return self;
    } /// 布局控件
    - (void)layoutSubviews {
    [super layoutSubviews]; // 设置按钮的 frame
    for (int i = 0; i < self.subviews.count - 1; i++) { // 列数
    NSInteger cols = 3; // 设置按钮尺寸
    CGFloat W = self.bounds.size.width;
    CGFloat H = self.bounds.size.height;
    CGFloat btnW = W / 5;
    CGFloat btnH = H / 5; // 计算按钮的 x 坐标值
    NSUInteger col = i % cols;
    CGFloat btnX = col * btnW * 2; // 计算按钮的 y 坐标值
    NSUInteger row = i / cols;
    CGFloat btnY = row * btnH * 2; // 设置按钮的 frame
    UIButton *btn = self.subviews[i + 1];
    btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    } // 设置提示信息框的 frame
    self.alertLabel.frame = CGRectMake(0, -50, self.bounds.size.width, 30);
    } /// 触摸开始
    - (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event { // 获取触摸起始点位置
    CGPoint startPoint = [touches.anyObject locationInView:self]; // 获取其 button
    UIButton *button = nil;
    for (UIButton *btn in self.subviews) { // 设置触摸按钮的灵敏度
    CGRect frame = btn.frame;
    CGRect tmpFrame = CGRectMake(frame.origin.x + frame.size.width / 4,
    frame.origin.y + frame.size.height / 4,
    frame.size.width / 2,
    frame.size.height / 2); // 判断某点在不在其 frame 上
    if (CGRectContainsPoint(tmpFrame, startPoint)) {
    button = btn;
    }
    } // 选中此 button
    if (button && button.selected == NO) {
    button.selected = YES;
    [self.selectedArray addObject:button];
    }
    } /// 触摸移动
    - (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event { // 获取触摸点位置
    CGPoint touchPoint = [touches.anyObject locationInView:self]; // 获取其 button
    UIButton *button = nil;
    for (UIButton *btn in self.subviews) { // 设置触摸按钮的灵敏度
    CGRect frame = btn.frame;
    CGRect tmpFrame = CGRectMake(frame.origin.x + frame.size.width / 4,
    frame.origin.y + frame.size.height / 4,
    frame.size.width / 2,
    frame.size.height / 2); // 判断某点在不在其 frame 上
    if (CGRectContainsPoint(tmpFrame, touchPoint)) {
    button = btn;
    }
    } // 选中此 button
    if (button && button.selected == NO) {
    button.selected = YES;
    [self.selectedArray addObject:button];
    } else {
    self.currentPoint = touchPoint;
    } // 刷新视图
    [self setNeedsDisplay];
    } /// 触摸结束
    - (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event { if (self.selectedArray.count < 4) { // 触摸点数过少
    if (self.resultBlock) {
    self.resultBlock(NO, @"请至少连续连接四个点");
    } for (UIButton *btn in self.selectedArray) { btn.highlighted = YES;
    btn.selected = NO;
    } // 延迟
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
    (int64_t)(0.5 * NSEC_PER_SEC)),
    dispatch_get_main_queue(), ^{ [self clearPath];
    }); } else { // 触摸完成
    if (self.resultBlock) { // 获取触摸结果
    NSMutableString *path = [NSMutableString string];
    for (UIButton *btn in self.selectedArray) {
    [path appendFormat:@"%ld", btn.tag];
    } // 对滑动获取的密码值进行 MD5 加密
    NSString *md5Path = [path q_md5String]; self.resultBlock(YES, md5Path);
    } [self clearPath];
    }
    } /// 触摸取消
    - (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event { [self touchesEnded:touches withEvent:event];
    } /// 绘制贝塞尔连接线
    - (void)drawRect:(CGRect)rect { if (self.selectedArray.count == 0) {
    return;
    } UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 5;
    path.lineJoinStyle = kCGLineCapRound;
    [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] set]; for (int i = 0; i < self.selectedArray.count; i++) { UIButton *btn = self.selectedArray[i]; // 如果是第一个按钮,则将其曲线的起点放在其按钮上,否则则进行连线
    if (i == 0) {
    [path moveToPoint:btn.center];
    } else {
    [path addLineToPoint:btn.center];
    }
    } // 如果不满足上述条件,按钮不存在则连接到临时点
    [path addLineToPoint:self.currentPoint];
    [path stroke];
    } /// 清除连接线
    - (void)clearPath { // 取消选中按钮
    for (UIButton *btn in self.selectedArray) {
    btn.highlighted = NO;
    btn.selected = NO;
    } // 清空选中按钮
    [self.selectedArray removeAllObjects]; // 刷新视图
    [self setNeedsDisplay];
    } /// 懒加载
    - (NSMutableArray *)selectedArray {
    if (_selectedArray == nil) {
    _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
    } @end
  • ViewController.m

    	// 设置 frame
    CGFloat margin = 50;
    CGFloat width = self.view.bounds.size.width - margin * 2;
    CGRect frame = CGRectMake(margin, 200, width, width); // 创建手势锁视图界面,获取滑动结果
    QTouchLockView *touchLockView = [QTouchLockView q_touchLockViewWithFrame:frame
    pathResult:^(BOOL isSucceed, NSString * _Nonnull result) { // 处理手势触摸结果
    [self dealTouchResult:result isSucceed:isSucceed];
    }]; [self.view addSubview:touchLockView]; - (void)dealTouchResult:(NSString *)result isSucceed:(BOOL)isSucceed { // 处理手势触摸结果 if (isSucceed) { // 判读密码是否存在
    NSUserDefaults *df = [NSUserDefaults standardUserDefaults]; if ([df objectForKey:@"touchLock"] == nil) { // 设置手势锁 [self.passWordArrM addObject:result]; if (self.passWordArrM.count == 1) {
    self.touchLockView.alertLabel.text = @"请再设置一次";
    } if (self.passWordArrM.count == 2) {
    if ([self.passWordArrM[0] isEqualToString:self.passWordArrM[1]]) { // 存储密码
    [df setValue:self.passWordArrM[0] forKey:@"touchLock"];
    [df synchronize]; self.touchLockView.alertLabel.text = @"手势密码设置成功"; } else { // 两次滑动结果不一致
    [self.passWordArrM removeAllObjects]; self.touchLockView.alertLabel.text = @"两次滑动的结果不一致,请重新设置";
    }
    } } else { // 解锁 if ([result isEqualToString:[df objectForKey:@"touchLock"] ]) {
    self.touchLockView.alertLabel.text = @"解锁成功";
    } else {
    self.touchLockView.alertLabel.text = @"密码不正确,请重试";
    }
    } } else { // 滑动点数过少
    self.touchLockView.alertLabel.text = result;
    }
    }
  • 效果

iOS - TouchLock 手势解锁的更多相关文章

  1. iOS绘制手势解锁密码

    手势解锁这个功能其实已经用的越来越少了.但是郁闷不知道我公司为什么每次做一个app都要把手势解锁加上.....于是就自己研究了一下手势解锁页面的实现.. 要想实现这个页面,先说说需要掌握哪些: UIP ...

  2. iOS - TouchLock 手势锁

    1.绘制手势锁 具体实现代码见 GitHub 源码 QExtension QTouchLockView.h @interface QTouchLockView : UIView /// 提示信息框 @ ...

  3. [iOS UI进阶 - 5.0] 手势解锁Demo

    A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件   code source: https://github.com/hellovoidworld/GestureUnlockDemo     B ...

  4. SJGestureUnlock快速集成手势解锁

    前言:如果页面显示不完整或图片看不了还请移步:简书 SJGestureUnlock.h 常用自定义属性 @interface SJGestureUnlock : UIView @property (n ...

  5. Quartz2D复习(二) --- 手势解锁

    这次支付宝手机客户端升级,把手势解锁那个功能去掉了,引起很多人的抱怨,觉得少了手势解锁的保护,个人信息容易泄漏了... 那么手势解锁功能是怎么是实现的呢,这里使用Quart2D来简单模拟一下, 先看下 ...

  6. HTML5实现屏幕手势解锁

    HTML5实现屏幕手势解锁(转载) https://github.com/lvming6816077/H5lockHow to use? <script type="text/java ...

  7. iOS--开发之手势解锁

    本文主要介绍通过手势识别实现手势解锁功能,这个方法被广泛用于手机解锁,密码验证,快捷支付等功能实现.事例效果如下所示. 首先,我们先分析功能的实现过程,首先我们需要先看大致的实现过程: 1.加载九宫格 ...

  8. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...

  9. [BS-25] IOS中手势UIGestureRecognizer概述

    IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...

随机推荐

  1. [转]Win7下安装配置sharepoint server 2010

    转自:http://blog.sina.com.cn/s/blog_5d93d7aa010151lp.html 要开发SharePoint 2010应用程序,开发人员必须构建一个SharePoint ...

  2. memcached启动参数

    memcached启动参数描述: -d :启动一个守护进程, -m:分配给Memcache使用的内存数量,单位是MB,默认是64MB, -u :运行Memcache的用户 -l :监听的服务器IP地址 ...

  3. 启动hadoop报192.168.1.151: Address 192.168.1.151 maps to node1, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!

    使用root用户启动hadoop的时候报错: [root@node1 ~]# su - hadoop -c start-all.sh starting namenode, logging to /ap ...

  4. HDU 2222:Keywords Search(AC自动机模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...

  5. JavaScript 网址

    1. javascript 模板引擎 http://aui.github.io/artTemplate/

  6. python: hashlib 加密模块

    加密模块hashlib import hashlib m=hashlib.md5() m.update(b'hello') print(m.hexdigest()) #十六进制加密 m.update( ...

  7. intellij idea 10.5介绍

    IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.Ant.JUn ...

  8. 使用Window Live Writer写博客

    1.打开“日志账户”—>“日志选项”. 2.点击“更新账户信息”. 3.输入博客地址,用户名和密码,点击“下一步”. 4.耐心等待片刻... 5.设置“日志昵称”,点击“完成”. 这样就大功告成 ...

  9. 转载-python学习笔记之输入输出功能读取和写入数据

    读取.写入和 Python 在 “探索 Python” 系列以前的文章中,学习了基本的 Python 数据类型和一些容器数据类型,例如tuple.string 和 list.其他文章讨论了 Pytho ...

  10. hdu 3398

    String Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...