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

效果图:

需要用到的代码:

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. 转:UML工具Astah的使用

    原文链接:http://blog.csdn.net/vipygd/article/details/9182247 前言 UML是软件工程中非常重要的知识点.我们经常要去展示各种UML图,当然,我们要将 ...

  2. eclipse安装emmet插件

    http://www.cnblogs.com/matchless/archive/2013/04/10/3011973.html

  3. 4、一、Introduction(入门):3、System Permissions(系统权限)

    3.System Permissions(系统权限)   Android is a privilege-separated operating system, in which each applic ...

  4. ElasticSearch(一)-- 简介

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  5. Flask web开发之路二

    今天创建第一个flask项目,主app文件代码如下: # 从flask这个框架导入Flask这个类 from flask import Flask #初始化一个Flask对象 # Flasks() # ...

  6. PHP基础语法 【】【】 PHP函数

    <?php //注释语法 /* 多行注释 */ //输出语法 echo "hello"; echo "world","dhakj"; ...

  7. Maven Web项目部署到Tomcat下问题

    但是也遇到了很多问题,下面记录一下Web项目部署到Tomcat下的问题 1.普通的WEB项目,就是虽然是用maven搭建的,但是没有使用profiles.xml文件来配置参数.这样的项目可以通过以下的 ...

  8. 对类型“Func<,>”的引用声称该类型是在“mscorlib”中定义的,但未能找到

    报错 右击→属性

  9. CSS3 transform 属性

    CSS3 transform 属性 语法: transform: none|transform-functions; 值 描述 none 定义不进行转换. matrix(n,n,n,n,n,n) 定义 ...

  10. qs.parse()、qs.stringify()使用方法

    qs是一个npm仓库所管理的包,可通过npm install qs命令进行安装. 1. qs.parse()将URL解析成对象的形式 const Qs = require('qs'); let url ...