美文网首页
DrawRect绘图实现手势密码控件

DrawRect绘图实现手势密码控件

作者: 李昭宏 | 来源:发表于2017-01-16 00:33 被阅读34次

公司项目中除了之前的指纹解锁外,还有手势解锁,这就扯到了手势解锁的功能实现

最终效果如此

其实核心就是利用touchBegin,touchMoved和touchEnded三个事件,记录点击的按钮,并且绘制出线条即可

代码如下
ViewController.m

//
//  ViewController.m
//  TestPractise
//
//  Created by 李昭宏 on 2017/1/13.
//  Copyright © 2017年 scut. All rights reserved.
//

#import "ViewController.h"
#import "GestureView.h"

@interface ViewController ()

@property (strong, nonatomic) GestureView * gestureView;

@end

@implementation ViewController

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.view.backgroundColor = [UIColor whiteColor];
    [self configView];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private method

- (void)configView {
    self.gestureView = [[GestureView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds),CGRectGetHeight([UIScreen mainScreen].bounds))];
    [self.view addSubview:self.gestureView];
}

@end

核心 手势密码控件
GestureView.m

//
//  GestureView.m
//  TestPractise
//
//  Created by 李昭宏 on 2017/1/16.
//  Copyright © 2017年 scut. All rights reserved.
//

#import "GestureView.h"

@interface GestureView()

@property (nonatomic , strong) NSMutableArray *buttonArray;

@property (nonatomic , assign) CGPoint movePoint;

@end

@implementation GestureView {
    UIColor *selectColor;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configView];
        
        selectColor = [self colorWithHexString:@"0x33a3ff"];
    }
    return self;
}

- (UIColor *)colorWithHexString:(NSString *)color{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    // strip "0X" or "#" if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}


- (void)configView {
    self.backgroundColor = [self colorWithHexString:@"f5f5f5"];
    
    for (int i = 0; i < 9; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        // 设置普通状态下的图片
        [button setImage:[UIImage imageNamed:@"gestureButtonNormal"] forState:UIControlStateNormal];
        
        [button setImage:[UIImage imageNamed:@"gestureButtonSelect"] forState:UIControlStateSelected];
        
        // 不允许用户交互
        button.userInteractionEnabled = NO;
        
        [self addSubview:button];
        
        CGFloat btnW = 64;
        CGFloat btnH = 64;
        CGFloat btnX = 0;
        CGFloat btnY = 0;
        
        CGFloat tolCol = 3;
        CGFloat margin = ([UIScreen mainScreen].bounds.size.width - tolCol * btnW) / (tolCol + 1);
        
        // 给按钮设置位置
        button.tag = i;
        NSInteger col = i % 3;
        NSInteger row = i / 3;
        btnX = margin + (margin + btnW) * col;
        btnY = margin + (margin + btnH) * row;
        
        button.frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}

// 获取触摸点
- (CGPoint)pointWithTouches:(NSSet *)touches {
    // 当前触摸点
    UITouch *touch = [touches anyObject];
    return  [touch locationInView:self];
}

// 获取触摸按钮
- (UIButton *)buttonWithPoint:(CGPoint)point {
    for (UIButton *button in self.subviews) {
        if (CGRectContainsPoint(button.frame, point)) { // 点在按钮上
            return button;
        }
    }
    return nil;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    // 当前触摸点
    CGPoint point = [self pointWithTouches:touches];
    // 获取触摸按钮
    UIButton *button = [self buttonWithPoint:point];
    
    if (button && button.selected == NO) { // 有触摸按钮的时候才需要选中
        
        button.selected = YES;
        [self.buttonArray addObject:button];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 当前触摸点
    CGPoint point = [self pointWithTouches:touches];
    
    self.movePoint = point;
    // 获取触摸按钮
    UIButton *button = [self buttonWithPoint:point];
    
    if (button && button.selected == NO) { // 有触摸按钮的时候才需要选中
        
        button.selected = YES;
        [self.buttonArray addObject:button];
    }
    // 重绘
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (!self.buttonArray.count) {
        return;
    }
    
    if (self.buttonArray.count<4) {
        NSLog(@"至少需要连接4个点");
    } else {
        NSMutableString *str = [NSMutableString string];
        for (UIButton *btn in self.buttonArray) {
            [str appendFormat:@"%ld",(long)btn.tag];
        }
        NSLog(@"手势轨迹%@",str);
    }

    for (UIButton *button in self.buttonArray) {
        button.selected = NO;
    }

    [self.buttonArray removeAllObjects];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    if (!self.buttonArray.count)return;
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    for (int i = 0; i < self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        
        
        if (i == 0) {
            [path moveToPoint:button.center];
        } else {
            [path addLineToPoint:button.center];
        }
    }
    
    // 所有选中按钮之间都连线
    [path addLineToPoint:self.movePoint];
    
    //
    [selectColor setStroke];
    path.lineWidth = 3;
    path.lineJoinStyle = kCGLineJoinRound;
    // 渲染到视图
    [path stroke];
}

#pragma mark - getter

- (NSMutableArray *)buttonArray {
    if (_buttonArray) {
        return _buttonArray;
    }
    
    _buttonArray = [NSMutableArray array];
    return _buttonArray;
}

@end

相关文章

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

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

  • 手势锁与涂鸦画板

    gestureLock(手势锁)实现步骤 布局控件:背景控件和手势锁控件 手势锁控件:子控件:循环增加9个按钮,设...

  • drawRect

    为什么要实现drawRect:方法才能绘图到view上? drawRect:方法在什么时候被调用?

  • iOS 手势密码

    源码参考:链接密码:37gm 源码可实现设置手势密码、登陆验证手势密码、修改手势密码 另外添加了钥匙串本地保存手势...

  • 手势密码-iOS

    手势密码实现效果:

  • wrs-gesturelockview

    前言 手势密码控件,支持密码设置、密码修改、密码校验 功能 支持密码设置、密码修改、密码校验 密码设置或修改回调,...

  • 视图绘制

    视图绘制是调用drawRect:方法来实现的。对于AppKit中的各种界面控件,系统默认实现了不同控件的界面绘制和...

  • Flutter 手势密码控件

    一个Flutter编写的手势识别验证锁。 例子 设置密码例子 验证密码例子 基本思想 先使用画布绘制背景: 九个圆...

  • drawRect:绘图

    drawRect:绘图 作用:专门用来绘图 什么时候调用:当View显示的时候调用(ViewWillAppear和...

  • 画图+截图+水印

    绘图需在- (void)drawRect:(CGRect)rect方法中实现 第一次显示和调用setNeedsDi...

网友评论

      本文标题:DrawRect绘图实现手势密码控件

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