2016-1-10 手势解锁demo的实现
一:实现自定义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的实现的更多相关文章
- [iOS UI进阶 - 5.0] 手势解锁Demo
A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件 code source: https://github.com/hellovoidworld/GestureUnlockDemo B ...
- iOS-高仿支付宝手势解锁(九宫格)
概述 高仿支付宝手势解锁, 通过手势枚举去实现手势密码相对应操作. 详细 代码下载:http://www.demodashi.com/demo/10706.html 基上篇[TouchID 指纹解锁] ...
- iOS--开发之手势解锁
本文主要介绍通过手势识别实现手势解锁功能,这个方法被广泛用于手机解锁,密码验证,快捷支付等功能实现.事例效果如下所示. 首先,我们先分析功能的实现过程,首先我们需要先看大致的实现过程: 1.加载九宫格 ...
- ReactNative手势解锁(react-native-ok-gesture-password)
在大前端的趋势之下,我也慢慢开始从事React Native相关的开发.但是奈何React Native生态相对于Android来说还是太小了.许多开源的库早早就已经不再维护.之前项目中需要用到手势解 ...
- canvas手势解锁源码
先放图 demo.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- SJGestureUnlock快速集成手势解锁
前言:如果页面显示不完整或图片看不了还请移步:简书 SJGestureUnlock.h 常用自定义属性 @interface SJGestureUnlock : UIView @property (n ...
- 微信iphone7、 ios10播放视频解决方案 2016.11.10
2016.11.10日更新以下方法 微信最新出同层播放规范 即使是官方的也无法解决所有android手机的问题. 另外iphone 5 .5s 某些手机始终会弹出播放,请继续采用 “以下是老的解决办法 ...
- Quartz2D复习(二) --- 手势解锁
这次支付宝手机客户端升级,把手势解锁那个功能去掉了,引起很多人的抱怨,觉得少了手势解锁的保护,个人信息容易泄漏了... 那么手势解锁功能是怎么是实现的呢,这里使用Quart2D来简单模拟一下, 先看下 ...
- HTML5实现屏幕手势解锁
HTML5实现屏幕手势解锁(转载) https://github.com/lvming6816077/H5lockHow to use? <script type="text/java ...
随机推荐
- linux笔记:RPM软件包管理-rpm命令管理
rpm包命名原则: rpm包的依赖性: 包名和包全名: rpm软件包安装.升级和卸载: rpm软件包查询: 从rpm包中提取指定文件:
- Mybatis学习(壹)
一.Mybatis的引言 1.Mybatis框架概念:是数据库持久层的框架,对数据库的访问和操作.Mybatis对JDBC的封装,Mybatis替换JDBC开发,解决DAO中的通用问题. 2.JDBC ...
- ubuntu下python3安装scrapy,OpenSSL
环境:ubuntu 16.04 , python3.5.1+ 安装顺序如下: sudo apt-get install build-essential sudo apt-get install p ...
- addViewController之后view里面的点击事件不响应
let dealsSeeMoreViewController = DealsSeeMoreViewController(owner: self) self.dealsStackView.addArra ...
- OTA(空中下载技术)
选自:http://baike.baidu.com/link?url=bKOx4Gcgefi17Zt09pRA6zI-p7zzKVkoN07khRVTPfUtqyRMzdG5xURfpnp3wSP_A ...
- Objective-C:Foundation框架-常用类-NSValue
NSNumber是NSValue的子类,前者只能包装数字,后者可以包装任意值.NSArray.NSDictionary只能存储OC对象,不能存储结构体.因此,如果想要在NSArray.NSDictio ...
- 如何给一个网卡配置多个虚拟ip
1.执行命令 ifconfig etho: 192.168.1.101 netmask 255.255.255.0 up 2.要想永久保存,则将刚刚那行代码写入/etc/rc.local (开机都会 ...
- 好用的json-path
$.store.book[?(@.price < 10)].title Here is a complete overview and a side by side comparison of ...
- 深入理解Redis:底层数据结构
简介 redis[1]是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorte ...
- 如何在 CentOS 中设置 NTP 服务器
网络时间协议(NTP)用来同步网络上不同主机的系统时间.你管理的所有主机都可以和一个指定的被称为 NTP 服务器的时间服务器同步它们的时间.而另一方面,一个 NTP 服务器会将它的时间和任意公共 NT ...