本文实例为大家分享了ios手势密码解锁的相关代码,供大家参考,具体内容如下
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
|
//
// lockview.m
// 手势解锁
//
// created by daniel on 16/4/4.
// copyright © 2016年 daniel. all rights reserved.
//
#import "lockview.h"
@interface lockview ()
/** 保存已选中的按钮 */
@property(nonatomic, strong) nsmutablearray *selectedbtn;
/** 当前触摸点 */
@property(nonatomic, assign) cgpoint curp;
@end
@implementation lockview
- (nsmutablearray *)selectedbtn {
if (_selectedbtn == nil) {
_selectedbtn = [nsmutablearray array];
}
return _selectedbtn;
}
- (ibaction)pan:(uipangesturerecognizer *)pan {
//获取当前触摸点
_curp = [pan locationinview:self];
//判断触摸点在不在按钮上
for (uibutton *btn in self.subviews) {
//如果在按钮上就设置选中状态
//触摸点必须在button中心点30范围内才选中,更精确,体验更好
cgrect rect = cgrectmake(btn.center.x, btn.center.y, 30, 30);
if (cgrectcontainspoint(rect, _curp) && btn.selected == no) {
btn.selected = yes;
//将这个选中的按钮保存起来
[self.selectedbtn addobject:btn];
}
}
//重绘,调用drawrect方法
[self setneedsdisplay];
//手指抬起时
if (pan.state == uigesturerecognizerstateended) {
nsmutablestring *strm = [nsmutablestring string];
//如果当前触摸点不在button上,则手指抬起时,就显示选中的button以及连线
uibutton *lastbtn = [self.selectedbtn lastobject];
//把最后一个选中按钮的中心点设置为当前触摸点,清除最后多出来的一截连线
_curp = lastbtn.center;
for (uibutton *btn in self.selectedbtn) {
//保存手势密码
[strm appendformat:@ "%ld" , btn.tag];
}
//strm就是密码了
nslog(@ "%@" ,strm);
//todo:对比之前保存的密码,如果对了就直接跳转界面了
//nslog(@"手指抬起");
//这里了执行完之后系统自动调用了重绘方法
//等待2s后清除所有连线,清除button的选中状态
dispatch_after(dispatch_time(dispatch_time_now, (int64_t)(1.2 * nsec_per_sec)), dispatch_get_main_queue(), ^{
//取消按钮选中
//[self.selectedbtn makeobjectsperformselector:@selector(setselected:) withobject:no];这句不知道为啥没有效果,只好用循环了
for (uibutton *btn in self.selectedbtn) {
[btn setselected:no];
}
//清除连线,清空选中按钮
[self.selectedbtn removeallobjects];
[self setneedsdisplay];
});
}
}
- ( void )drawrect:(cgrect)rect {
nsinteger count = self.selectedbtn.count;
//如果没有按钮被选中,就不画线
if (count == 0) {
return ;
}
uibezierpath *path = [uibezierpath bezierpath];
//把所有点都连完线
for ( int i = 0; i < count; i++) {
uibutton * btn = self.selectedbtn[i];
if (i == 0) {
//如果是第一个点,就设置为起点
[path movetopoint:btn.center];
} else {
//添加连线
[path addlinetopoint: btn.center];
}
}
//画最后一个点到手指触摸点之间的线
[path addlinetopoint:_curp];
[[uicolor greencolor]set];
path.linejoinstyle = kcglinejoinround;
path.linewidth = 8;
[path stroke];
}
- ( void )awakefromnib {
//创建9个按钮
for ( int i = 0; i < 9; i++) {
uibutton *btn = [uibutton buttonwithtype:uibuttontypecustom];
//不允许与用户交互,也就是点击事件不作处理
btn.userinteractionenabled = no;
btn.tag = i;
[btn setimage:[uiimage imagenamed:@ "gesture_node_normal" ] forstate:uicontrolstatenormal];
[btn setimage:[uiimage imagenamed:@ "gesture_node_highlighted" ] forstate:uicontrolstateselected];
[self addsubview:btn];
}
}
- ( void )layoutsubviews {
[super layoutsubviews];
//布局子控件
nsinteger count = self.subviews.count;
//列数
int cols = 3;
cgfloat x = 0;
cgfloat y = 0;
cgfloat w = 74;
cgfloat h = 74;
//间距
cgfloat margin = (self.bounds.size.width - w * cols) / (cols + 1);
cgfloat col = 0;
cgfloat row = 0;
for (nsinteger i = 0; i < count; i++) {
uibutton *btn = self.subviews[i];
//计算当前button的列行以及xy值
col = i % cols;
row = i / cols;
x = margin + col * (margin + w);
y = row * (margin + w);
btn.frame = cgrectmake(x, y, w, h);
}
}
@end
|
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助。