iOS实现九宫格连线手势解锁

2025-05-29 0 66

本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下

效果图:

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

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

216
//

// clockview.m

// 手势解锁

//

// created by llkj on 2017/8/24.

// copyright © 2017年 laynecheung. all rights reserved.

//

#import "clockview.h"

@interface clockview ()

//存放当前选中的按钮

@property (nonatomic, strong) nsmutablearray *selectbtnarry;

//当前手指所在点

@property (nonatomic, assign) cgpoint curp;

@end

@implementation clockview

- (void)awakefromnib{

[super awakefromnib];

//初始化

[self setup];

}

- (nsmutablearray *)selectbtnarry{

if (_selectbtnarry == nil) {

_selectbtnarry = [nsmutablearray array];

}

return _selectbtnarry;

}

- (void)setup{

for (int i = 0; i < 9; i ++) {

//创建按钮

uibutton *btn = [uibutton buttonwithtype:uibuttontypecustom];

btn.tag = i;

btn.userinteractionenabled = no;

[btn setimage:[uiimage imagenamed:@"gesture_node_normal"] forstate:uicontrolstatenormal];

[btn setimage:[uiimage imagenamed:@"gesture_node_selected"] forstate:uicontrolstateselected];

[self addsubview:btn];

}

}

//获取当前点

- (cgpoint)getcurrentpoint:(nsset *)point{

uitouch *touch = [point anyobject];

return [touch locationinview:self];

}

//返回按钮

- (uibutton *)btnrectcontainspoint:(cgpoint)point{

//遍历brn判断当前点在不在btn上

for (uibutton *btn in self.subviews) {

if (cgrectcontainspoint(btn.frame, point)) {

return btn;

}

}

return nil;

}

- (void)touchesbegan:(nsset<uitouch *> *)touches withevent:(uievent *)event{

//1.获取当前点

cgpoint curp = [self getcurrentpoint:touches];

//2.判断当前点在不在btn上

uibutton *btn = [self btnrectcontainspoint:curp];

if (btn && btn.selected == no) {

btn.selected = yes;

//保存选中的按钮

[self.selectbtnarry addobject:btn];

}

}

- (void)touchesmoved:(nsset<uitouch *> *)touches withevent:(uievent *)event{

//1.获取当前点

cgpoint curp = [self getcurrentpoint:touches];

self.curp = curp;

//2.判断当前点在不在btn上

uibutton *btn = [self btnrectcontainspoint:curp];

if (btn && btn.selected == no) {

btn.selected = yes;

//保存选中的按钮

[self.selectbtnarry addobject:btn];

}

//重绘

[self setneedsdisplay];

}

- (void)touchesended:(nsset<uitouch *> *)touches withevent:(uievent *)event{

nsmutablestring *str = [nsmutablestring string];

//1.取消所有选中的按钮

for (uibutton *btn in self.selectbtnarry) {

btn.selected = no;

[str appendformat:@"%ld", btn.tag];

}

//2.清空路径

[self.selectbtnarry removeallobjects];

[self setneedsdisplay];

//查看是否是第一次设置密码

nsstring *keypwd = [[nsuserdefaults standarduserdefaults] objectforkey:@"keypwd"];

if (!keypwd) {

[[nsuserdefaults standarduserdefaults] setobject:str forkey:@"keypwd"];

[[nsuserdefaults standarduserdefaults] synchronize];

uialertview *alertv = [[uialertview alloc] initwithtitle:@"第一次设置密码成功" message:nil delegate:nil cancelbuttontitle:@"确定" otherbuttontitles:nil, nil];

[alertv show];

nslog(@"第一次输入密码");

}else{

if ([keypwd isequaltostring:str]) {

nslog(@"密码正确");

uialertview *alertv = [[uialertview alloc] initwithtitle:@"手势输入正确" message:nil delegate:nil cancelbuttontitle:@"确定" otherbuttontitles:nil, nil];

[alertv show];

}else{

nslog(@"密码错误");

uialertview *alertv = [[uialertview alloc] initwithtitle:@"手势输入错误" message:nil delegate:nil cancelbuttontitle:@"确定" otherbuttontitles:nil, nil];

[alertv show];

}

}

//3.查看当前选中按钮的顺序

nslog(@"选中按钮顺序为:%@",str);

}

- (void)drawrect:(cgrect)rect{

if (self.selectbtnarry.count) {

//1.创建路径

uibezierpath *path = [uibezierpath bezierpath];

//2.取出所有保存的按钮

for (int i = 0; i < self.selectbtnarry.count; i ++) {

uibutton *btn = self.selectbtnarry[i];

//当前按钮是不是第一个按钮

if (i == 0) {

//设置成路径的起点

[path movetopoint:btn.center];

} else {

//添加一根线到按钮中心

[path addlinetopoint:btn.center];

}

}

//添加一根线到当前手指所在点

[path addlinetopoint:self.curp];

//设置线宽/颜色

[path setlinewidth:5];

[[uicolor whitecolor] set];

[path setlinejoinstyle:kcglinejoinround];

//3.绘制路径

[path stroke];

}

}

- (void)layoutsubviews{

[super layoutsubviews];

cgfloat x = 0;

cgfloat y = 0;

cgfloat btnwh = 75;

int column = 3;

int margin = (self.bounds.size.width - (column * btnwh)) / (column + 1);

int currentcolumn = 0;

int currentrow = 0;

for (int i = 0; i < self.subviews.count; i ++) {

// 求当前所在的列

currentcolumn = i % column;

// 求当前所在的行

currentrow = i / column;

x = margin + (btnwh + margin) * currentcolumn;

y = margin + (btnwh + margin) * currentrow;

uibutton *btn = self.subviews[i];

btn.frame = cgrectmake(x, y, btnwh, btnwh);

}

}

@end

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS实现九宫格连线手势解锁 https://www.kuaiidc.com/89098.html

相关文章

发表评论
暂无评论