iOS实现手势密码功能
发布时间 - 2026-01-11 00:06:43 点击率:次手势密码实现

手势密码 一般常常用于金融项目,做的是安全相关的业务。具体实现如下思路,我把它分为view层和逻辑层。我将数据层合并到view层中了,最好是加上数据层用于处理加密的密码和密码的存储
view层
view层主要处理,包括(九个按钮)touchesBegan,touchesMoved,touchesEnded,点与点之间画线,手指滑动画线,画线主要是在drawRect中重绘,提到这里必须不能忘记setNeedsDisplay这个方法。还要记录经过的按钮btnsArray(存放按钮的数组),这个 可以和相关的具体值做映射,也可以直接设置btn 的tag,还要添加完成绘画的回调。提供给逻辑层去处理。
逻辑层
用于处理完成交互后的业务,包括(请求接口,异常逻辑显示,等等)
具体的demo点这里
具体的code:
view.h
//
// YHGesturePasswordView.h
// 手势密码
//
// Created by mrlee on 2017/3/5.
// Copyright © 2017年 mrlee. All rights reserved.
//
typedef enum {
GestureSetPassword, //设置手势密码
GestureResultPassword //已有手势密码教验
} PasswordState;
//设置密码的3种状态
typedef enum {
FristPwd, //第一次设置密码
PwdNoValue, //二次设置密码不一致
SetPwdSuccess, //设置密码成功
Other
}SetPwdState;
#import <UIKit/UIKit.h>
@interface YHGesturePasswordView : UIView
/** btn图片*/
@property (nonatomic,strong)UIImage *btnImage;
///选中的图片
@property (nonatomic,strong)UIImage *btnSelectImage;
///划线颜色
@property (nonatomic,strong)UIColor *lineColor;
/** 解锁手势完成之后判断结果时调用的block */
@property (nonatomic,copy)BOOL (^sendReaultData)(NSString *str);
//设置手势密码
@property(nonatomic,copy)void(^setPwdBlock)(SetPwdState pwdState);
// init
-(instancetype)initWithFrame:(CGRect)frame WithState:(PasswordState)state;
@end
view.m
//
// YHGesturePasswordView.m
// 手势密码
//
// Created by mrlee on 2017/3/5.
// Copyright © 2017年 mrlee. All rights reserved.
//
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#import "YHCustomButton.h"
#import "YHGesturePasswordView.h"
#import <CommonCrypto/CommonDigest.h>
@interface YHGesturePasswordView(){
/** 判断是当设置密码用,还是解锁密码用*/
PasswordState Amode;
}
/** 所有的按钮集合*/
@property (nonatomic,strong)NSMutableArray * allBtnsArray;
/** 解锁时手指经过的所有的btn集合*/
@property (nonatomic,strong)NSMutableArray * btnsArray;
/** 手指当前的触摸位置*/
@property (nonatomic,assign)CGPoint currentPoint;
@end
@implementation YHGesturePasswordView
-(instancetype)initWithFrame:(CGRect)frame WithState:(PasswordState)state{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
Amode = state;
for (int i = 0; i<9; i++) {
YHCustomButton *btn = [[YHCustomButton alloc]init];
[btn setTag:i];
btn.userInteractionEnabled = NO;
if (self.lineColor == nil) {
self.lineColor = [UIColor greenColor];
}
[self addSubview:btn];
}
}
return self;
}
-(void)drawRect:(CGRect)rect{
// 每次调用这个方法的时候如果背景颜色是default会产生缓存,如果设置了颜色之后就没有缓存,绘制之前需要清除缓存
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);//清空上下文
for (int i = 0; i<self.btnsArray.count; i++) {
UIButton *btn = self.btnsArray[i];
if (i == 0) {
CGContextMoveToPoint(ctx, btn.center.x, btn.center.y);
}else{
CGContextAddLineToPoint(ctx, btn.center.x, btn.center.y);
}
}
if (!CGPointEqualToPoint(self.currentPoint, CGPointZero)) {//如果起点不是CGPointZero的话才来划线
CGContextAddLineToPoint(ctx, self.currentPoint.x, self.currentPoint.y);
}
CGContextSetLineWidth(ctx, 12);
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineJoin(ctx, kCGLineJoinRound);
[self.lineColor set];
CGContextStrokePath(ctx);
}
-(void)layoutSubviews{
[self.allBtnsArray removeAllObjects];
for (int index =0; index<self.subviews.count; index ++) {
if ([self.subviews[index] isKindOfClass:[YHCustomButton class]]) {
[self.allBtnsArray addObject:self.subviews[index]];
}
}
// button 绘制九宫格
[self drawUi];
}
#pragma mark Private method
-(void)drawUi{
for (int index = 0; index<self.allBtnsArray.count; index ++) {
//拿到每个btn
UIButton *btn = self.subviews[index];
//设置frame
CGFloat btnW = 74;
CGFloat btnH = 74;
CGFloat margin = (SCREEN_WIDTH - (btnW *3))/4;
//x = 间距 + 列号*(间距+btnW)
CGFloat btnX = margin + (index % 3)*(margin + btnW);
CGFloat btnY = margin + (index / 3)*(margin + btnH);
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
}
//设置密码
-(SetPwdState)pwdValue:(NSString *)str{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"pwdValue"] == nil) {
//第一次设置
[[NSUserDefaults standardUserDefaults] setValue:str forKey:@"pwdValue"];
return FristPwd;
}
if ([str isEqualToString: [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdValue"]]) {
//设置成功
return SetPwdSuccess;
}
if (![str isEqualToString: [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdValue"]]) {
//二次设置不一样
return PwdNoValue;
}
return Other;
}
//清空
-(void)clear{
[self.btnsArray removeAllObjects];
self.currentPoint = CGPointZero;
[self setNeedsDisplay];
self.lineColor = [UIColor greenColor];
self.userInteractionEnabled = YES;
}
//获取触摸的点
-(CGPoint)getCurrentTouch:(NSSet<UITouch*> *)touches{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
return point;
}
-(UIButton *)getCurrentBtnWithPoint:(CGPoint) currentPoint{
for (UIButton *btn in self.subviews) {
if (CGRectContainsPoint(btn.frame, currentPoint)) {
return btn;
}
}
return nil;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [self getCurrentTouch:touches];
UIButton *btn = [self getCurrentBtnWithPoint:point];
if (btn && btn.selected != YES) {
btn.selected = YES;
[self.btnsArray addObject:btn];
NSLog(@" array is value %@",self.btnsArray);
}
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint movePoint = [self getCurrentTouch:touches];
UIButton *btn = [self getCurrentBtnWithPoint:movePoint];
if (btn && btn.selected !=YES) {
btn.selected = YES;
[self.btnsArray addObject:btn];
NSLog(@"btn is value %@",self.btnsArray);
}
self.currentPoint = movePoint;
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
for (UIButton *btn in self.btnsArray) {
[btn setSelected:NO];
}
NSMutableString *result = [NSMutableString string];
for (UIButton *btn in self.btnsArray) {
[result appendString: [NSString stringWithFormat:@"%ld",(long)btn.tag]];
}
switch (Amode) {
case GestureSetPassword:{
//设置手势密码
self.setPwdBlock([self pwdValue:result]);
}
break;
case GestureResultPassword :{
//获取手势密码结果
if (self.sendReaultData) {
if (self.sendReaultData(result) == YES) {
NSLog(@"success");
[self clear];
}else{
NSLog(@"手势有误");
}
}
}
break;
default:
break;
}
//返回结果
[self clear];
}
#pragma mark 延时加载
-(NSMutableArray *)btnsArray{
if (_btnsArray == nil) {
_btnsArray = [NSMutableArray array];
}
return _btnsArray;
}
-(NSMutableArray *)allBtnsArray{
if (_allBtnsArray == nil) {
_allBtnsArray = [NSMutableArray array];
}
return _allBtnsArray;
}
@end
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# iOS
# 手势密码
# IOS开发笔记之禁用手势滑动返回功能的示例
# IOS中多手势之间的冲突和解决办法
# iOS 用Swipe手势和动画实现循环播放图片示例
# 利用iOS手势与scrollView代理实现图片的放大缩小
# IOS中的七种手势小结
# IOS开发之手势响应事件优先级的实例详解
# 解锁
# 画线
# 清空
# 的是
# 是在
# 点这里
# 已有
# 把它
# 可以直接
# 我将
# 中了
# 提供给
# 回调
# 才来
# 大家多多
# 并到
# 加载
# 九宫格
# str
# void
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
PHP 500报错的快速解决方法
JavaScript数据类型有哪些_如何准确判断一个变量的类型
Android滚轮选择时间控件使用详解
Swift中循环语句中的转移语句 break 和 continue
如何在景安云服务器上绑定域名并配置虚拟主机?
微信小程序 require机制详解及实例代码
Swift开发中switch语句值绑定模式
Laravel如何使用Livewire构建动态组件?(入门代码)
如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南
如何安全更换建站之星模板并保留数据?
javascript中的try catch异常捕获机制用法分析
python中快速进行多个字符替换的方法小结
高配服务器限时抢购:企业级配置与回收服务一站式优惠方案
公司门户网站制作流程,华为官网怎么做?
如何快速生成高效建站系统源代码?
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
在线教育网站制作平台,山西立德教育官网?
如何快速查询网站的真实建站时间?
IOS倒计时设置UIButton标题title的抖动问题
html5的keygen标签为什么废弃_替代方案说明【解答】
如何在Windows环境下新建FTP站点并设置权限?
如何用花生壳三步快速搭建专属网站?
品牌网站制作公司有哪些,买正品品牌一般去哪个网站买?
EditPlus中的正则表达式 实战(4)
Laravel如何使用集合(Collections)进行数据处理_Laravel Collection常用方法与技巧
北京网页设计制作网站有哪些,继续教育自动播放怎么设置?
HTML5段落标签p和br怎么选_文本排版常用标签对比【解答】
详解jQuery中的事件
HTML5建模怎么导出为FBX格式_FBX格式兼容性及导出步骤【指南】
浅谈javascript alert和confirm的美化
Laravel怎么使用artisan命令缓存配置和视图
Laravel怎么清理缓存_Laravel optimize clear命令详解
Edge浏览器如何截图和滚动截图_微软Edge网页捕获功能使用教程【技巧】
Microsoft Edge如何解决网页加载问题 Edge浏览器加载问题修复
七夕网站制作视频,七夕大促活动怎么报名?
PHP怎么接收前端传的文件路径_处理文件路径参数接收方法【汇总】
如何在阿里云虚拟主机上快速搭建个人网站?
消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工
Laravel任务队列怎么用_Laravel Queues异步处理任务提升应用性能
,网页ppt怎么弄成自己的ppt?
如何用搬瓦工VPS快速搭建个人网站?
Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明
Laravel如何使用Passport实现OAuth2?(完整配置步骤)
Laravel如何配置中间件Middleware_Laravel自定义中间件拦截请求与权限校验【步骤】
Laravel如何实现数据导出到CSV文件_Laravel原生流式输出大数据量CSV【方案】
JS中页面与页面之间超链接跳转中文乱码问题的解决办法
黑客如何利用漏洞与弱口令入侵网站服务器?
Laravel怎么实现搜索功能_Laravel使用Eloquent实现模糊查询与多条件搜索【实例】
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
新三国志曹操传主线渭水交兵攻略
下一篇:yii 组件如何自定义
下一篇:yii 组件如何自定义

