手势解锁(Quartz2D)

效果图.png

1、实现

  • 1.自定义解锁视图LockView
#import <UIKit/UIKit.h>
@interface LockView : UIView

@end

// .m文件实现
#import "LockView.h"

#define kColsCount 3
#define kBtnCount 9
#define kMargin 20


@interface LockView ()

@property (nonatomic, strong) NSMutableArray *selectedBtns; // 记录当前所有选中的按钮

@property (nonatomic, assign) CGPoint currentPoint; // 当前选中按钮的Point

@end

@implementation LockView

- (void)drawRect:(CGRect)rect{
    
    if (_selectedBtns.count <= 0) {
        return;
    }

    UIBezierPath *path = [[UIBezierPath alloc] init];

    for (int i = 0; i< self.selectedBtns.count; i++) {
        UIButton *btn = self.selectedBtns[i];
        if (i == 0) {
            [path moveToPoint:btn.center];
        }else{
            [path addLineToPoint:btn.center];
        }
    }
    
    path.lineWidth = 15;
    path.lineJoinStyle = kCGLineCapRound;
    
    // 我在绘画结束后,将属性currentPoint设为CGPointZero了,因为如果如果在距前一个选中点绘画过程中,并没有连接下一个点,改线段不应该绘画
    if (self.currentPoint.x != CGPointZero.x && self.currentPoint.y != CGPointZero.y) {
        [path addLineToPoint:self.currentPoint];
    }

    [[UIColor greenColor] set];

    [path stroke];
}


/**
 *  懒加载
 *
 *  @return 返回当前选中按钮
 */
- (NSArray *)selectedBtns{
    
    if (_selectedBtns == nil) {
        _selectedBtns = [NSMutableArray array];
    }
    return _selectedBtns;
}


/**
 *  添加所有子控件按钮
 */
- (void)addAllButtons{
    
    for (int i = 0; i<kBtnCount; i++) {

        UIButton *btn = [[UIButton alloc] init];
        btn.tag = i;
        
        UIImage *imageNormal = [UIImage imageNamed:@"gesture_node_normal"];
        
         btn.adjustsImageWhenHighlighted = NO; // 去除高亮调整图片
        [btn setImage:imageNormal forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];

        /*
        // 使用按钮点击时间,点击下去是鼠标抬起才会设置选中,但是不符合我的要求,我要的是鼠标进过就选中,所有就需要在视图上看如果经过按钮区域就选中即可。
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
         */
        
        btn.userInteractionEnabled = NO; // 设置按钮不能用户交互,然后在touchMoved方法中判断按钮选中状态
        
        [self addSubview:btn];
    }
}


//- (void)btnClick:(UIButton *)btn{
//    btn.selected = YES;
//}

/**
 *  布局子控件
 */
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat w = (self.frame.size.width - (kColsCount+1)*kMargin) / kColsCount;
    CGFloat h = w;
    CGFloat btnW = 74;
    CGFloat btnH = 74;
    CGFloat x = 0.0;
    CGFloat y = 0.0;

    for (int i = 0; i<self.subviews.count; i++) {
        
        int col = i % kColsCount;
        int row = i / kColsCount;

        x = (kMargin + w) * col + kMargin;
        y = (h + kMargin) * row + kMargin;
        
        UIButton *btn = self.subviews[i];
        btn.frame = CGRectMake(x, y, btnW, btnH);
    }
}


/**
 *  initWithFrame:添加子控件
 */
- (nonnull instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self addAllButtons]; // 添加子控件
    }
    return self;
}


// xib加载完调用,添加子控件
- (void)awakeFromNib{
    [self addAllButtons]; // 添加子控件
}

// 在移动中,判断点是否在按钮上,在,选中按钮,并将其添加到选中按钮的数组中记录下来.
- (void)touchesMoved:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    
    UITouch *touch = [touches anyObject];
    
    
    
    CGPoint curP = [touch locationInView:self];
    
    _currentPoint = curP;
    
    // 转换点到按钮相对
    for (UIButton *btn in self.subviews) {
        CGPoint point = [self convertPoint:curP toView:btn];
       BOOL isContainP = [btn pointInside:point withEvent:event];
        
        if (isContainP && btn.selected == NO) {
            btn.selected = YES;
            
            [self.selectedBtns addObject:btn];
        }

    }
    
    // 重绘
    [self setNeedsDisplay];
}

- (void)touchesEnded:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
    
     // 绘画结束,将属性currentPoint设为CGPointZero了,因为如果如果在距前一个选中点绘画过程中,并没有连接下一个点,改线段不应该绘画
    _currentPoint = CGPointZero;
    [self setNeedsDisplay];
}
@end
  • 2.布局解锁视图,在控制器的view中,拖拽一个view控件,然后修改custom Class 为 LockView,运行就可以看见解锁视图效果了
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,536评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,253评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,572评论 7 249
  • 缘分真的很奇妙,它可以让两个不相干的人连在一起,也可以让亲密的人分开!我们能做的就是珍惜现在!在缘分来临之际,不后悔!
    蓝堇l佳阅读 93评论 0 0
  • 持续打卡已经第八天啦,有些日记没有公开,只是个人碎碎念而已,有一些观念和想法还是想留给自己。 参与这个打卡我觉得对...
    KC可阅读 270评论 0 0