OC屏幕手势解锁
感觉屏幕解锁好像很牛的样子,所以试着写了一个,代码很简单,手势用到的也是原生的,如果该代码帮助了你,记得点赞,如果该代码有任何问题,也可以随时和我联系。改代码用到的两张图片,是我随便找的两张,可以自行找图片代替
效果图:

需要用到的代码:

ScreenLockView.h
#import <UIKit/UIKit.h> @interface ScreenLockView : UIView
@property(nonatomic,copy)void(^blockScreenPas)(NSString *pas);
@end
ScreenLockView.m
#import "ScreenLockView.h" #define Row 3 //3行
#define Col 3 //3列
#define Width 50 //这边宽度和高度一样,都设置为50 @interface ScreenLockView ()
@property(nonatomic,strong)NSMutableArray *choseArr;
@property(nonatomic,assign)CGPoint currPoint;
@end @implementation ScreenLockView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
self.backgroundColor = [UIColor whiteColor]; self.choseArr = [NSMutableArray array];
for(int i = 0 ; i < 9 ; i++){ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
int row = i / Row;//行
int col = i % Col;//列 //行间距
int rowMargin = (frame.size.width - Width * Row)/(Row-1);
//列间距
int colMargin = (frame.size.height - Width * Col)/(Col-1);
btn.frame = CGRectMake((Width+rowMargin)*col, (colMargin+Width)*row, Width, Width); [btn setImage:[UIImage imageNamed:@"noChose"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"chose"] forState:UIControlStateSelected];
btn.tag = i;
[self addSubview:btn];
btn.userInteractionEnabled = NO;
}
}
return self;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoine = [touch locationInView:self];
[self isContainsWithPoint:touchPoine]; }
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoine = [touch locationInView:self];
[self isContainsWithPoint:touchPoine];
self.currPoint = touchPoine;
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
NSString *pas = @"";
for(UIButton *btn in self.choseArr){
btn.selected = NO;
pas = [pas stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)btn.tag]];
}
if(self.blockScreenPas){
self.blockScreenPas(pas);
} [self.choseArr removeAllObjects];
[self setNeedsDisplay]; }
/**
判断point是否被UIButton的frame包含
*/
-(void)isContainsWithPoint:(CGPoint)point{
for(UIButton *btn in [self subviews]){
if(CGRectContainsPoint(btn.frame ,point )&& ![self.choseArr containsObject:btn]){
[self.choseArr addObject:btn];
btn.selected = YES; }
} } - (void)drawRect:(CGRect)rect {
UIBezierPath * path = [UIBezierPath bezierPath];
for(int i=0;i<self.choseArr.count;i++){
UIButton *btn = self.choseArr[i];
if(i == 0){
[path moveToPoint:btn.center];//起始点
}else{
[path addLineToPoint:btn.center];
}
} [path addLineToPoint:self.currPoint]; [path setLineWidth:2.f];
[[UIColor redColor] set];
[path stroke]; } @end
使用:
ScreenLockView *lockView = [[ScreenLockView alloc]initWithFrame:CGRectMake(0, 0, 260, 260)];
lockView.center = self.view.center;
[self.view addSubview:lockView]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 260, 30)];
label.textAlignment = NSTextAlignmentCenter;
label.center = CGPointMake(self.view.center.x, lockView.frame.origin.y+lockView.frame.size.height+50);
[self.view addSubview:label]; lockView.blockScreenPas = ^(NSString *pas) {
label.text = pas;
};
OC屏幕手势解锁的更多相关文章
- HTML5实现屏幕手势解锁
HTML5实现屏幕手势解锁(转载) https://github.com/lvming6816077/H5lockHow to use? <script type="text/java ...
- HTML5实现屏幕手势解锁(转载)
来源:https://github.com/lvming6816077/H5lockhttp://threejs.org/examples/http://www.inf.usi.ch/phd/wett ...
- iOS-高仿支付宝手势解锁(九宫格)
概述 高仿支付宝手势解锁, 通过手势枚举去实现手势密码相对应操作. 详细 代码下载:http://www.demodashi.com/demo/10706.html 基上篇[TouchID 指纹解锁] ...
- SJGestureUnlock快速集成手势解锁
前言:如果页面显示不完整或图片看不了还请移步:简书 SJGestureUnlock.h 常用自定义属性 @interface SJGestureUnlock : UIView @property (n ...
- Quartz2D复习(二) --- 手势解锁
这次支付宝手机客户端升级,把手势解锁那个功能去掉了,引起很多人的抱怨,觉得少了手势解锁的保护,个人信息容易泄漏了... 那么手势解锁功能是怎么是实现的呢,这里使用Quart2D来简单模拟一下, 先看下 ...
- iOS--开发之手势解锁
本文主要介绍通过手势识别实现手势解锁功能,这个方法被广泛用于手机解锁,密码验证,快捷支付等功能实现.事例效果如下所示. 首先,我们先分析功能的实现过程,首先我们需要先看大致的实现过程: 1.加载九宫格 ...
- 2016-1-10 手势解锁demo的实现
一:实现自定义view,在.h,.m文件中代码如下: #import <UIKit/UIKit.h> @class ZLLockView; @protocol ZLLockViewDele ...
- iOS绘制手势解锁密码
手势解锁这个功能其实已经用的越来越少了.但是郁闷不知道我公司为什么每次做一个app都要把手势解锁加上.....于是就自己研究了一下手势解锁页面的实现.. 要想实现这个页面,先说说需要掌握哪些: UIP ...
- [iOS UI进阶 - 5.0] 手势解锁Demo
A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件 code source: https://github.com/hellovoidworld/GestureUnlockDemo B ...
随机推荐
- 多密钥ssh-key生成与管理
由于 git 大文件用 http 方式难以传输,必须使用 ssh-key,而 ssh-key 又生成了好多个.最近在各种折腾 ssh,公钥私钥上花费了很多时间,现将一些问题总结如下.系统为 Mac/L ...
- Linux-C实现GPRS模块发送短信
“GSM模块,是将GSM射频芯片.基带处理芯片.存储器.功放器件等集成在一块线路板上,具有独立的操作系统.GSM射频处理.基带处理并提供标准接口的功能模块.GSM模块根据其提供的数据传输速率又可以分为 ...
- Ubuntu14.04下安装redis-3.2.0以及开机自启动
去官网下载Redis-3.2.0.tar.gz,将redis-3.2.0.tar.gz放入/opt目录下 解压redis-3.2.0.tar.gz xiaoyao@xiaoyao-virtual-ma ...
- F - Fibonacci again and again
任何一个大学生对菲波那契数列(Fibonacci numbers)应该都不会陌生,它是这样定义的: F(1)=1; F(2)=2; F(n)=F(n-1)+F(n-2)(n>=3); 所以,1, ...
- linux 下 sublime配置
sublime3 import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_p ...
- 通过Java语言连接mysql数据库
1加载驱动 2创建链接对象 3创建语句传输对象 4接受结果集 5遍历 6关闭资源
- [Asp.net]Uploadify上传大文件,Http error 404 解决方案 - wolfy
引言 之前使用Uploadify做了一个上传图片并预览的功能,今天在项目中,要使用该插件上传大文件.之前弄过上传图片的demo,就使用该demo进行测试.可以查看我的这篇文章: [Asp.net]Up ...
- CCPC-Wannafly Winter Camp Day4 G---置置置换【递推】【组合数】【逆元】
置置置换 已经提交 已经通过 63.89% Total Submission:72 Total Accepted:46 题目描述 wlswlswls有一个整数nnn,他想请你算一下有多少1...n1. ...
- python中的os
import sys, os print(__file__) # 绝对路径,实际是文件名 /Users/majianyu/Desktop/test/bin/bin.py print(os.path.a ...
- HDU 5985/nowcoder 207D - Lucky Coins - [概率题]
题目链接:https://www.nowcoder.com/acm/contest/207/D 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5985 ...