iOS简单手势解锁
发布日期:2022-03-18 08:27:37 浏览次数:39 分类:技术文章

本文共 2359 字,大约阅读时间需要 7 分钟。

看了下XX学院的视频,简单记录下思路:

  1. 使用UIButton九宫格布局
  2. 重写touch事件,获取点击或者经过的按钮,改变按钮的状态
  3. 使用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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:如何使用Github?
下一篇:iOS Apps间分享数据

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月03日 17时44分22秒