欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-21 08:27:00
本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手势解锁//// created by l...

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

demo下载地址:

效果图:

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

核心代码:

//
// 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

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