手势解锁

2016-12-23  本文已影响40人  LeapDing

首先看下我们要制作功能的效果如图所示:

效果图效果图

思路介绍

节点类

@interface DYHLockNodeView ()

/**
 手势外部的空心圆
 */
@property (nonatomic, strong) CAShapeLayer *outlineLayer;

/**
 手势内部的实心圆
 */
@property (nonatomic, strong) CAShapeLayer *innerCircleLayer;

@end

- (void)layoutSubviews {
    
    self.outlineLayer.frame = self.bounds;
    UIBezierPath *outlinePath = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    self.outlineLayer.path = outlinePath.CGPath;
    
    //中心圆的直径为外部圆的1/3
    CGFloat innerWidth = self.bounds.size.width / 3;
    self.innerCircleLayer.frame = CGRectMake(innerWidth, innerWidth, innerWidth, innerWidth);
    UIBezierPath *innerPath = [UIBezierPath bezierPathWithOvalInRect:self.innerCircleLayer.bounds];
    self.innerCircleLayer.path = innerPath.CGPath;
}

- (void)setStatusNormal {
    
    self.outlineLayer.strokeColor = [UIColor whiteColor].CGColor;
    self.innerCircleLayer.fillColor = [UIColor clearColor].CGColor;
}

- (void)setStatusSelected {
    
    self.outlineLayer.strokeColor = LIGHTBLUE.CGColor;
    self.innerCircleLayer.fillColor = LIGHTBLUE.CGColor;
}

- (void)setStatusWarning {
    
    self.outlineLayer.strokeColor = [UIColor redColor].CGColor;
    self.innerCircleLayer.fillColor = [UIColor redColor].CGColor;
}

密码视图类

初始化添加9宫格节点,然后在layoutSubviews中布局

[self.layer addSublayer:self.polygonLineLayer];
    
    _nodes = [NSMutableArray arrayWithCapacity:9];
    for (int i = 0; i < 9; i++) {
        DYHLockNodeView *nodeView = [[DYHLockNodeView alloc] init];
        nodeView.tag = i;
        [_nodes addObject:nodeView];
        [self addSubview:nodeView];
    }
    
    _selectedNodes = [NSMutableArray arrayWithCapacity:9];
    _points = [NSMutableArray array];

绘制九宫格

- (void)layoutSubviews {
    
    //添加一个mask层
    self.polygonLineLayer.frame = self.bounds;
    CAShapeLayer *maskLayer = [CAShapeLayer new];
    maskLayer.frame = self.bounds;
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:self.bounds];
    maskLayer.fillRule = kCAFillRuleEvenOdd;
    maskLayer.lineWidth = 1.0f;
    maskLayer.strokeColor = [UIColor blackColor].CGColor;
    maskLayer.fillColor = [UIColor blackColor].CGColor;
    
    //每行3个nodeView,没列3个,上下左右间距为一个nodeView的宽
    for (int i = 0; i < self.nodes.count; i++) {
        DYHLockNodeView *nodeView = self.nodes[i];
        CGFloat min = self.bounds.size.width < self.bounds.size.height ? self.bounds.size.width : self.bounds.size.height;
        CGFloat width = min / 5;
        CGFloat height = width;
        int row = i % 3;
        int column = i / 3;
        CGRect frame = CGRectMake(row * (width * 2), column * (width * 2), width, height);
        nodeView.frame = frame;
        [maskPath appendPath:[UIBezierPath bezierPathWithOvalInRect:frame]];
    }
    
    maskLayer.path = maskPath.CGPath;
    self.polygonLineLayer.mask = maskLayer;
}

手势滑动判断滑动位置

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = touches.anyObject;
    CGPoint touchPoint = [touch locationInView:self];
    // 查找手指滑过的node
    NSInteger index = [self indexForNodeAtPoint:touchPoint];
    if (index >= 0) {
        DYHLockNodeView *node = self.nodes[index];
        
        if (![self addSelectedNode:node]) {
            //移动线到手势位置
            [self moveLineWithFingerPosition:touchPoint];
        }
        
    } else {
        [self moveLineWithFingerPosition:touchPoint];
    }
}

手势滑动结束

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    //手势结束的时候,清除超出节点的线,起点在第一个节点中心,结束也是最后一个节点中心,超出部分不需要绘制
    [self removeLastFingerPosition];
    if([self.delegate respondsToSelector:@selector(lockView:didEndSwipeWithPassword:)]) {
        
        NSMutableString *password = [NSMutableString new];
        for(DYHLockNodeView *nodeView in self.selectedNodes) {
            NSString *index = [@(nodeView.tag) stringValue];
            [password appendString:index];
        }
        self.viewState = [self.delegate lockView:self didEndSwipeWithPassword:password];
        
    } else {
        self.viewState = DYHLockViewStateSelected;
    }
}

代码下载地址:https://github.com/iosyueshen/DYHLockViewDemo.git

上一篇 下一篇

猜你喜欢

热点阅读