AI智能
改变未来

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自定义UIButton九宫格效果
  • iOS实现九宫格自动生成视图
  • 关于iOS自带九宫格拼音键盘和Emoji表情之间的一些坑
  • IOS 九宫格布局实现方法
  • iOS仿邮箱大师的九宫格手势密码解锁
  • 深入解析iOS应用开发中九宫格视图布局的相关计算方法
  • 详解iOS应用UI开发中的九宫格坐标计算与字典转换模型
  • 使用Swift代码实现iOS手势解锁、指纹解锁实例详解
  • iOS实现手势解锁操作
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » iOS实现九宫格连线手势解锁