美文网首页
手势解锁

手势解锁

作者: LeapDing | 来源:发表于2016-12-23 17:42 被阅读40次

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

效果图效果图

思路介绍

  • 手势密码一般为9宫格模式,通过手势滑动设置一个多边形(polygon)的密码图片。
  • 9宫格的每一个格子可以用按钮表示节点,也可以用CAShapeLayer来绘制节点
  • 在这里采用CAShapeLayer来绘制9宫格的节点,手势密码的节点有选中模式正常模式警告模式三种状态,特别说明:警告模式是在第二次密码输入与第一次密码不一致,或者解锁密码错误的时候提示的状态。正常模式只是一个空心圆选中模式是一个内部的实心圆+外部的空心圆警告模式只是改变选中模式的颜色,所以这里采用两个CAShapeLayer对象来绘制外部空心圆内部实心圆(outlineLayerinnerCircleLayer)。
  • 创建节点类LockNodeView和密码视图类LockView,LockView采用maskView方式添加密码视图,9宫格用[maskPath appendPath:[UIBezierPath bezierPathWithOvalInRect:frame]来绘制,frame为每个节点的坐标,9个节点通过UIBezierPath统一绘制成一个图形,这样有利于管理。
  • 重写视图的-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event方法,也可以用滑动手势代替。
  • 滑动的时候去判断手势坐标滑到了哪一个节点,并且判断节点是否已经被选中,未选中添加连线。
  • 在滑动结束的时候获取保存节点数组,连接节点tag组成一个字符串,作为手势结果密码

节点类

@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

相关文章

  • iOS指纹解锁和手势解锁

    iOS指纹解锁和手势解锁 iOS指纹解锁和手势解锁

  • DrawRect绘图实现手势密码控件

    公司项目中除了之前的指纹解锁外,还有手势解锁,这就扯到了手势解锁的功能实现 其实核心就是利用touchBegin,...

  • 手势解锁

    金融产品手势解锁是常见的东西了,这里把我自己实现的记录一下。 自定义View的流程一般都是onMeasure跟on...

  • 手势解锁

    分析界面,当手指在上面移动时,当移动到一个按钮范围内当中, 它会把按钮给成为选中的状态.并且把第一个选中的按钮当做...

  • 手势解锁

    效果 实现思路 1.继承UIview 包含 所有子按钮的数组 按钮设置不可交互 有选中 和 未选中的图片2.布局...

  • 手势解锁

    首先看下我们要制作功能的效果如图所示: 思路介绍 手势密码一般为9宫格模式,通过手势滑动设置一个多边形(polyg...

  • 手势解锁

    下班回家,随便写写,写了个手势解锁,很多app都有。自己封装了一个,手势解锁视图。代码如下:头文件 源文件: 使用...

  • 手势解锁

  • 手势解锁

    手势解锁 界面搭建 自定义控制器的view 只要在view上面画东西,就要用到drawRect方法 加载图片 九宫...

  • 手势解锁

    1.最终效果图 2.思路: 2.1.首先把界面搭建起来:以绘制九宫格的方式,在view上绘制出九个button 2...

网友评论

      本文标题:手势解锁

      本文链接:https://www.haomeiwen.com/subject/lhgkvttx.html