一:实现自定义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. H5网页播放器播不了服务器上的mp4视频文件

    打开IIS,在功能视图里找到MIME类型菜单,打开该菜单后鼠标右键添加.mp4扩展名的MIME类型video/mp4 其他视频文件播放不了估计也得在IIS里添加对应的MIME类型(从服务器下载文件时也 ...

  2. Qt之QSequentialAnimationGroup

    简述 QSequentialAnimationGroup类提供动画的串行组. QSequentialAnimationGroup是一个串行运行动画的QAnimationGroup,在另一个动画播放结束 ...

  3. uva---(11549)CALCULATOR CONUNDRUM

    Problem C CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She ...

  4. web开发必须知道的javascripat工具

    1,JavaScript compressor and comparison tool 有许多工具可以帮助你压缩JavaScript代码,但是这个过程比较耗时,并且,对于某个特定的场景来说,很难分析出 ...

  5. CSS 层叠及样式表来源

    Web标准化运动的口号——分离.分离.分离. 在2003年的 SXSW 会议中, Steve Champeon 和 Nick Finck 做了一个名为“面向未来的全方位 Web 设计”的演讲,揭示了这 ...

  6. 转:Nginx+Apache环境的安装与配置

    转:http://www.server110.com/nginx/201404/8817.html 我们依然尽可能采用yum来安装我们需要的软件,由系统官方维护的软件,其安全性和稳定性都值得信赖,并且 ...

  7. IKAnalyzer 和 solr4.3 冲突

    solr4.3 运行之后发现异常:Exception in thread "main" java.lang.VerifyError: class org.wltea.analyze ...

  8. 转 Warning:MongoDB Replica Sets配置注意事项

    我们知道,MongoDB不提供单机的数据安全性,取而代之的是提供了Replica Sets的高可用方案.官方文档中提到的案例是三个节点组成的Replica Sets,这样在其中任何一个节点宕机后都会自 ...

  9. 执行MAVEN更新包

    我们一般使用 mvn eclipse:eclipse 执行对maven库的引用,这样会修改项目下的classpath文件. 我们修改直接在eclipse 使用maven库作为项目的引用. 步骤如下: ...

  10. 用js创建XMLHttpRequest对象池[转]

    //使用literal语法定义一个对象:XMLHttp var XMLHttp = { //定义第一个属性,该属性用于缓存XMLHttpRequest对象的数组 XMLHttpRequestPool: ...