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

效果图:

需要用到的代码:

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屏幕手势解锁的更多相关文章

  1. HTML5实现屏幕手势解锁

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

  2. HTML5实现屏幕手势解锁(转载)

    来源:https://github.com/lvming6816077/H5lockhttp://threejs.org/examples/http://www.inf.usi.ch/phd/wett ...

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

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

  4. SJGestureUnlock快速集成手势解锁

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

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

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

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

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

  7. 2016-1-10 手势解锁demo的实现

    一:实现自定义view,在.h,.m文件中代码如下: #import <UIKit/UIKit.h> @class ZLLockView; @protocol ZLLockViewDele ...

  8. iOS绘制手势解锁密码

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

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

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

随机推荐

  1. MTK 修改默认屏幕亮度

    frameworks\base\packages\SettingsProvider\res\values\defaults.xml <!-- Default screenbrightness, ...

  2. [Tensorflow] Object Detection API - build your training environment

    一.前期准备 Prepare protoc Download Protocol Buffers Create folder: protoc and unzip it. unsw@unsw-UX303U ...

  3. asp.net mvc 3.0 知识点整理 ----- (4).asp.net mvc 3 和asp.net mvc 4 对比

    asp.net mvc的版本更新很快,每个版本都在前一个版本的基础上,进行性能的优化和功能的完善和提升. 以下,便是我对比了下两个版本,发现最基本的差异.(更新补充中..) 一.关于配置类Global ...

  4. 父页面操作嵌套iframe子页面的HTML标签元素

    一个页面A.html使用iframe嵌套一个页面B.html,在A页面写js操作B页面HTML元素,首先要获取到B页面document对象,才能对嵌套页面进行操作 请看一个实例,在A页面写js操作B页 ...

  5. 服务器中了蠕虫病毒Wannamine2.0小记

    近期用户反馈某台服务器总感觉性能不是很好存在卡顿,于是今天远程上去分析. 打开任务管理器发现CPU使用率非常低,内存使用也在接受范围内(10/64G).不过我有一个偏好就是不喜欢用系统自带的任务管理器 ...

  6. Django之ORM版学员管理系统

    班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = models.CharF ...

  7. D - Game Prediction

    Suppose there are M people, including you, playing a special card game. At the beginning, each playe ...

  8. zgrep用法

    http://manpages.ubuntu.com/manpages/trusty/man1/zgrep.1.html NAME Zgrep - search compressed files fo ...

  9. J S 脚本语言 if() { if { } else { } } var a =100; switch { case ( ) break ; } 基础详解 , 最下面有例子

    注释语法 注释语法// 多行注释/ JS输出语句 JS样式尽量靠最下面写 <script type="text/javascript">//嵌入JS开始代码 //ale ...

  10. xcode reset 删除重新安装

    Type "rm -rf ~/Library/Application Support/Xcode" and press "Enter." This remove ...