一:实现自定义view,在.h,.m文件中代码如下:

#import <UIKit/UIKit.h>
@class ZLLockView;
@protocol ZLLockViewDelegate <NSObject>
- (void)lockView:(ZLLockView *)lockView didSelectedPwd: (NSString *)pwd;
@end
@interface ZLLockView : UIView
@property (nonatomic, weak) id<ZLLockViewDelegate> delegate; @end
//
// ZLLockView.m
// 手势解锁demo实现
//
// Created by Mac on 16/1/9.
// Copyright © 2016年 Mac. All rights reserved.
// #import "ZLLockView.h"
@interface ZLLockView()
@property (nonatomic, strong) NSMutableArray *btnsSelected; @property (nonatomic, assign) CGPoint lastPoint; @end
@implementation ZLLockView
- (NSMutableArray *)btnsSelected
{
if (!_btnsSelected) {
_btnsSelected = [NSMutableArray array];
}
return _btnsSelected;
}
- (instancetype)init
{
if (self = [super init]) {
[self setBtn];
}
return self;
}
- (void)setBtn
{
for (int i = ; i < ; i ++) {
UIButton *btn = [[UIButton alloc] init];
btn.tag = i; [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
btn.userInteractionEnabled = NO;
[self addSubview:btn];
}
} // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = ;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
[[UIColor blueColor] set];
NSInteger count = self.btnsSelected.count;
if (count ==)return;
for (NSInteger i = ; i < count; i ++) {
UIButton *btn = self.btnsSelected[i];
if (i == ) {
[path moveToPoint:btn.center];
}else{
[path addLineToPoint:btn.center];
}
}
[path addLineToPoint:self.lastPoint];
[path stroke];
} - (void)layoutSubviews
{
CGFloat btnW = ;
CGFloat btnH = ;
// 间距
CGFloat padding = (self.frame.size.width - btnW * ) / ;
NSInteger count = self.subviews.count;
for (NSInteger i = ; i < count; i ++) {
UIButton *btn = self.subviews[i];
// 当前按钮所处的列
NSInteger column = i % ;
// 计算btn的x值
CGFloat btnX = (column+) * padding + column * btnW;
// 当前按钮所处的行
CGFloat row = i / ;
CGFloat btnY = (row+) * padding + row * btnW;
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取当前点
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];
// 判断在不在范围内
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, location)) {//判断获得的点在不在范围内
//将选中的按钮放在数组里
if (btn.selected == NO) {
[self.btnsSelected addObject:btn];
}
[btn setSelected:YES];
}
else self.lastPoint = location;
}
[self setNeedsDisplay];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSMutableString *pwd = [NSMutableString string]; for (UIButton *btn in self.btnsSelected) {
[btn setSelected:NO];
//拼接选中按钮的索引
[pwd appendFormat:@"%ld",btn.tag];
[self setNeedsDisplay];
}
[self.btnsSelected removeAllObjects];
NSLog(@"%@",pwd);
[self.delegate lockView:self didSelectedPwd:pwd];
}
@end

二:在控制器中实现代理方法,代码如下:

//
// ViewController.m
// 手势解锁demo实现
//
// Created by Mac on 16/1/9.
// Copyright © 2016年 Mac. All rights reserved.
// #import "ViewController.h"
#import "ZLLockView.h"
#import "MBProgressHUD+CZ.h"
@interface ViewController ()<ZLLockViewDelegate>
@property (nonatomic, strong) ZLLockView *lockView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Home_refresh_bg"]];
// Do any additional setup after loading the view, typically from a nib.
ZLLockView *lockView = [[ZLLockView alloc] init];
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
lockView.frame = CGRectMake(, , screenW, screenW);
lockView.center = self.view.center;
lockView.backgroundColor = [UIColor clearColor];
[self.view addSubview:lockView];
self.lockView = lockView;
lockView.delegate = self; }
- (void) lockView:(ZLLockView *)lockView didSelectedPwd:(NSString *)pwd
{
if ([pwd isEqual:@""]) {
[MBProgressHUD showMessage:@"密码正确!"];
[self.lockView removeFromSuperview];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUD];
});
}else{
[MBProgressHUD showMessage:@"密码错误!"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[MBProgressHUD hideHUD];
});
}
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

三:效果:

2016-1-10 手势解锁demo的实现的更多相关文章

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

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

  2. iOS-高仿支付宝手势解锁(九宫格)

    概述 高仿支付宝手势解锁, 通过手势枚举去实现手势密码相对应操作. 详细 代码下载:http://www.demodashi.com/demo/10706.html 基上篇[TouchID 指纹解锁] ...

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

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

  4. ReactNative手势解锁(react-native-ok-gesture-password)

    在大前端的趋势之下,我也慢慢开始从事React Native相关的开发.但是奈何React Native生态相对于Android来说还是太小了.许多开源的库早早就已经不再维护.之前项目中需要用到手势解 ...

  5. canvas手势解锁源码

    先放图 demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  6. SJGestureUnlock快速集成手势解锁

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

  7. 微信iphone7、 ios10播放视频解决方案 2016.11.10

    2016.11.10日更新以下方法 微信最新出同层播放规范 即使是官方的也无法解决所有android手机的问题. 另外iphone 5 .5s 某些手机始终会弹出播放,请继续采用 “以下是老的解决办法 ...

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

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

  9. HTML5实现屏幕手势解锁

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

随机推荐

  1. 【bzoj1046】上升序列

    [bzoj1046]上升序列 题意 对于一个给定的S={a1,a2,a3,-,an},若有P={ax1,ax2,ax3,-,axm},满足(x1 < x2 < - < xm)且( a ...

  2. 关于MySQL回滚机制

    在事务中,每个正确的原子操作都会被顺序执行,直到遇到错误的原子操作,此时事务会将之前的操作进行回滚.回滚的意思是如果之前是插入操作,那么会执行删 除插入的记录,如果之前是update操作,也会执行up ...

  3. 爬虫基础学习 转【http://www.cnblogs.com/huangxincheng/archive/2012/11/08/2759752.html】

    这一篇我们聊聊在页面抓取时应该注意到的几个问题. 一:网页更新 我们知道,一般网页中的信息是不断翻新的,这也要求我们定期的去抓这些新信息,但是这个“定期”该怎么理解,也就是多长时间需要 抓一次该页面, ...

  4. GetMemory

    在函数中动态申请内存(虚拟内存,堆),利用指针返回值指向申请的内存.

  5. hdu------2488Tornado(几何)

    Tornado Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  6. SqlServer中把结果集放到到临时表的方法

    一. SELECT INTO   1. 使用select into会自动生成临时表,不需要事先创建   select * into #temp from sysobjects   01. 把存储过程结 ...

  7. 网页html结构右侧栏固定,左侧自适应大小。

    最近写了一个项目,写页面的结构,html树形结构是有header,container,footer部分,其中container部分是右侧栏是固定宽度,左侧是自适应宽度与屏幕高度. 第一次写的博客文章是 ...

  8. 集合框架,ArrayList和Vector的区别,让arrayList线程安全的几种方案

    boolean add(E e) 将指定的元素添加到此列表的尾部. void add(int index, E element) 将指定的元素插入此列表中的指定位置. boolean addAll(C ...

  9. 阮一峰:RSA算法原理(一)

    今天看到一篇好文章,关于加密算法,收藏了觉得不过瘾,还是自己贴一遍,也能加深一下印象. 原文链接:http://www.ruanyifeng.com/blog/2013/06/rsa_algorith ...

  10. class、interface、struct的区别

    1 struct和class有什么区别 1.1默认的继承访问权限 Struct是public的,class是private的. 你可以写如下的代码: struct A { char a; }; str ...