温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-03 09:16:51 来源:亿速云 阅读:360 作者:小猪 栏目:移动开发

这篇文章主要讲解了iOS如何实现九宫格连线手势解锁,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

效果图:

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

看完上述内容,是不是对iOS如何实现九宫格连线手势解锁有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI