iOS简单手势解锁
发布日期:2022-03-18 08:27:37
浏览次数:39
分类:技术文章
本文共 2359 字,大约阅读时间需要 7 分钟。
看了下XX学院的视频,简单记录下思路:
- 使用
UIButton
九宫格布局 - 重写touch事件,获取点击或者经过的按钮,改变按钮的状态
- 使用UIBezierPath绘制曲线
主要代码逻辑如下:
#pragma mark - - (CGPoint)pointWithTouch:(NSSet *)touches{ UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:touch.view]; return point;}- (UIButton *)buttonWithPoint:(CGPoint)point{ for (UIButton *btn in self.subviews) { if (CGRectContainsPoint(btn.frame, point)) { return btn; } } return nil;}#pragma mark - - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ CGPoint point = [self pointWithTouch:touches]; UIButton *btn = [self buttonWithPoint:point]; if (btn && !btn.selected) { btn.selected = YES; [self.selectedBtns addObject:btn]; }}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ CGPoint point = [self pointWithTouch:touches]; UIButton *btn = [self buttonWithPoint:point]; if (btn && !btn.selected) { btn.selected = YES; [self.selectedBtns addObject:btn]; }else{ self.currentPoint = point; } [self setNeedsDisplay];}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ if ([self.delegate respondsToSelector:@selector(lockView:didFinishPath:)]) { NSMutableString *path = [NSMutableString string]; for (UIButton *btn in self.selectedBtns) { [path appendFormat:@"%ld", (long)btn.tag]; } [self.delegate lockView:self didFinishPath:path]; } [self.selectedBtns makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)]; [self.selectedBtns removeAllObjects]; [self setNeedsDisplay];}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ [self touchesEnded:touches withEvent:event];}#pragma mark - draw- (void)drawRect:(CGRect)rect{ if (self.selectedBtns.count == 0) { return; } UIBezierPath *path = [UIBezierPath bezierPath]; path.lineWidth = 8; path.lineJoinStyle = kCGLineJoinRound; [[UIColor colorWithRed:32/255.0 green:210/255.0 blue:254/255.0 alpha:0.5] set]; for(int i = 0;i < self.selectedBtns.count; i++ ){ UIButton *button = self.selectedBtns[i]; if (i == 0) { [path moveToPoint:button.center]; }else{ [path addLineToPoint:button.center]; } } [path addLineToPoint:self.currentPoint]; [path stroke];}
效果如下:
转载地址:https://windzen.blog.csdn.net/article/details/51483216 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月03日 17时44分22秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
16-Python高阶函数-效率翻倍
2019-04-26
vue-05列表渲染自学视频教程
2019-04-26
vue-10 数据监测watch自学视频
2019-04-26
vue-01 创建你的第一个vue页面-视频教程
2019-04-26
vue-07 表单双向绑定-视频自学
2019-04-26
引用、指针和句柄的区别
2019-04-26
面向对象-搞定她-搞定程序
2019-04-26
面向对象(二)-搞定她-搞定程序
2019-04-26
用python做个九九乘法表-够简单,能学会
2019-04-26
面向对象第一步封装
2019-04-26
10张动图学会python循环与递归
2019-04-26
java中的继承详解
2019-04-26
手机学习利器Qpython
2019-04-26
手把手教你用python开发界面程序-Tkinter对新手友好
2019-04-26
Python100行-贪吃蛇小游戏
2019-04-26
50行python代码写个计算器教程
2019-04-26
21-Scrapy爬虫框架
2019-04-26
假期你又带学习资料回家了吗?
2019-04-26
pip高阶玩法,让python模块安装飞起来
2019-04-26
22-爬虫-使用scrapy工具
2019-04-26