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

所属分类: 软件编程 / IOS 阅读数: 1687
收藏 0 赞 0 分享

本文实例为大家分享了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 正则表达式判断手机号码、固话

本文主要介绍了iOS 正则表达式判断手机号码、固话,以及匹配是否是移动/联通/电信手机号的方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

运用iOS教你轻松制作音乐播放器

这篇文章主要教大家如何运用iOS轻松制作音乐播放器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS 二维码扫描和应用跳转

本文讲解如何使用原生框架实现二维码扫描功能,并且进行扫描后的项目跳转。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS 定制多样式二维码

最常见的二维码功能包括信息获取、网站跳转、电商交易、手机支付等等,其拥有密度小、信息容量大、容错能力强、成本低、制作难度低等优点。在移动开发中,二维码的地位也越来越重要,掌握二维码的基本操作是重要的本领之一。本文将讲解iOS定制二维码的步骤与方法。
收藏 0 赞 0 分享

iOS获取当前app的设备名称和版本号等内容

本文主要介绍了iOS获取当前app的设备名称和版本号等内容的方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS页面跳转及数据传递(三种)

本文主要介绍了iOS页面跳转的三种方法及数据传递的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS常用小功能(获得屏幕图像、压缩图片、加边框、调整label的size)

本文主要介绍了iOS常用小功能:获得屏幕图像,label的动态size,时间戳转化为时间,RGB转化成颜色,加边框,压缩图片,textfield的placeholder,图片做灰度处理的方法。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS实现一个简易日历代码

本篇文章主要介绍了iOS实现一个简易日历代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS仿微信摇一摇动画效果加震动音效实例

这篇文章主要介绍了iOS仿微信摇一摇动画效果加震动音效实例,详细介绍了微信摇一摇功能的实现原理,非常具有实用价值,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS上下文实现评价星星示例代码

这篇文章主要介绍了iOS上下文实现评价星星的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享
查看更多