iOS如何实现九宫格连线手势解锁-创新互联

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

创新互联建站成都网站建设按需开发网站,是成都网站制作公司,为成都OPP胶袋提供网站建设服务,有成熟的网站定制合作流程,提供网站定制设计服务:原型图制作、网站创意设计、前端HTML5制作、后台程序开发等。成都网站设计热线:13518219792

效果图:

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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:iOS如何实现九宫格连线手势解锁-创新互联
网站网址:http://hbruida.cn/article/dgihpj.html