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

 更新时间:2020年04月18日 15:53:12   作者:LayneCheung  
这篇文章主要为大家详细介绍了iOS实现九宫格连线手势解锁,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

Demo下载地址:手势解锁

效果图:

核心代码:

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

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

相关文章

  • IOS 开发之xcode对比两个分支中同一个文件

    IOS 开发之xcode对比两个分支中同一个文件

    这篇文章主要介绍了IOS 开发之xcode对比两个分支中同一个文件的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下
    2017-08-08
  • iOS设置UIButton文字显示位置和字体大小、颜色的方法

    iOS设置UIButton文字显示位置和字体大小、颜色的方法

    这篇文章给大家分享了iOS如何设置UIButton的文字显示位置和字体的大小、颜色,文中给出了示例代码,相信对大家的学习和理解很有帮助,有需要的朋友们下面来一起看看吧。
    2016-09-09
  • 关于ios配置微信config出现验签失败的问题解决

    关于ios配置微信config出现验签失败的问题解决

    这篇文章主要介绍了关于ios配置微信config出现验签失败的问题解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • iOS 开发之 - 关闭键盘 退出键盘 的5种方式

    iOS 开发之 - 关闭键盘 退出键盘 的5种方式

    这篇文章主要介绍了iOS 开发之 - 关闭键盘 退出键盘 的5种方式的相关资料,需要的朋友可以参考下
    2016-09-09
  • iOS实现带指引线的饼状图效果(不会重叠)

    iOS实现带指引线的饼状图效果(不会重叠)

    饼状图对大家来说应该都不陌生,下面这篇文章主要给大家介绍了关于iOS实现带指引线的饼状图效果(不会重叠)的相关资料,文章通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2018-04-04
  • iOS  隐藏导航条和状态栏实现方法

    iOS 隐藏导航条和状态栏实现方法

    这篇文章主要介绍了 iOS隐藏导航条和状态栏实现方法的相关资料,有时候根据需求开发APP 需要隐藏导航栏和状态栏,这里提供了实现方法需要的朋友可以参考下
    2016-11-11
  • iOS仿擦玻璃效果的实现方法

    iOS仿擦玻璃效果的实现方法

    最近在网上看到一个博客分享的这个效果很不错,就拿下来看看,结果看了好几遍也没完全看懂,再结合自己之前学的东西感觉不用这么复杂也能实现同样的效果,于是就开始动手了。现在将实现的步骤和示例代码分享给大家,有需要的朋友们可以参考借鉴。
    2016-10-10
  • iOS中tableview实现编辑、全选及删除等功能的方法示例

    iOS中tableview实现编辑、全选及删除等功能的方法示例

    这篇文章主要给大家介绍了关于iOS中tableview实现编辑、全选及删除等功能的相关资料,文中通过示例代码介绍的非常详细,不仅是介绍实现的方法,将实现过程中遇到的问题也都分享出来了,需要的朋友们下面来一起看看吧。
    2017-07-07
  • iOS 使用AFN上传视频到服务器示例代码

    iOS 使用AFN上传视频到服务器示例代码

    本篇文章主要介绍了iOS 使用AFN上传视频到服务器示例代码,具有一定的参考价值,有兴趣的可以了解一下。
    2017-04-04
  • 简单谈谈c/c++中#import、#include和@class的区别

    简单谈谈c/c++中#import、#include和@class的区别

    对于#import,我想做过iOS开发的人应该都不陌生。在开发过程中,当我们需要声明某一个类时,都需要去引用。而#imclude的话,在我们学习C时就已经知道了,他的作用也是引用声明的意思。在表面上他们的作用似乎都是一样的。但是在具体功能实现方式上,还是有着很大的区别。
    2018-01-01

最新评论