
iOS简单手势解锁
发布日期:2022-03-18 08:27:37
浏览次数:23
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2023年06月03日 15时39分44秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
python numpy.argsort 函数以及python numpy.random.choice函数
2019-12-22 11:35:33
feedforward network
2019-12-22 11:35:33
MySQL
2019-12-22 11:35:34
开闭原则「Open Close Principle」
2019-12-22 11:35:34
依赖倒置原则「Dependence Inversion Principle」
2019-12-22 11:35:34
Note-Concurrency
2019-12-22 11:35:32
CentOS7搭建Java开发环境
2019-12-22 11:35:32
微软的cpprestsdk真的不咋地
2019-12-22 11:35:32
antlr4 爬坑日记
2019-12-22 11:35:32
机器学习之训练集,测试集,验证集划分
2019-12-22 11:35:33
python broadcast 和 numpy.sum()函数
2019-12-22 11:35:33
如何在QT环境中使用openCV
2019-12-22 11:35:31
软件自动更新解决方案及QT实现(源码已上传)
2019-12-22 11:35:31
FFmpeg 入门(1):截取视频帧
2019-12-22 11:35:31
C++模板
2019-12-22 11:35:31
线程的切换,线程局部存储
2019-12-22 11:35:31
C++软件开发主要领域及方向
2019-12-22 11:35:31
Note-MySQL
2019-12-22 11:35:31
网站信息的采集系列(三)--百度搜索图片的采集及下载
2019-12-22 11:35:29
网站信息采集系统(四)--京东商品信息的采集
2019-12-22 11:35:30