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

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

创新互联建站是一家以成都网站建设、网页设计、品牌设计、软件运维、成都网站营销、小程序App开发等移动开发为一体互联网公司。已累计为成都白乌鱼等众行业中小客户提供优质的互联网建站和软件开发服务。

效果图:

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 *)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 *)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 *)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如何实现九宫格连线手势解锁
当前路径:http://pwwzsj.com/article/gpcgcc.html