服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现手势密码功能

iOS实现手势密码功能

2021-03-09 15:40YunHe_Lee IOS

这篇文章主要为大家详细介绍了iOS实现手势密码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

手势密码实现

手势密码 一般常常用于金融项目,做的是安全相关的业务。具体实现如下思路,我把它分为view层和逻辑层。我将数据层合并到view层中了,最好是加上数据层用于处理加密的密码和密码的存储

view层

view层主要处理,包括(九个按钮)touchesBegan,touchesMoved,touchesEnded,点与点之间画线,手指滑动画线,画线主要是在drawRect中重绘,提到这里必须不能忘记setNeedsDisplay这个方法。还要记录经过的按钮btnsArray(存放按钮的数组),这个 可以和相关的具体值做映射,也可以直接设置btn 的tag,还要添加完成绘画的回调。提供给逻辑层去处理。

逻辑层

用于处理完成交互后的业务,包括(请求接口,异常逻辑显示,等等)
具体的demo点这里

具体的code:
view.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03