代码笔记手势解锁

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码笔记手势解锁相关的知识,希望对你有一定的参考价值。

动画实现解锁-文件目录

技术分享


ViewController.h文件
//创建自定义的View,遵守协议,设置代理,实现代理方法
 1 #import "LYPaintView.h"
 2 
 3 #import "ViewController.h"
 4 
 5 @interface ViewController ()<LYPaintViewDelegate>
 6 
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
14     //加载子定的View
15     LYPaintView *paintView=[LYPaintView paintView];
16     //设置代理
17     paintView.delegate=self;
18     
19     paintView.center =self.view.center;
20     //添加View
21     [self.view addSubview:paintView];
22 }
23 //修改状态栏的状态
24 -(UIStatusBarStyle)preferredStatusBarStyle{
25     return UIStatusBarStyleLightContent;
26 }
27 //实现代理方法
28 -(BOOL)checkPwd:(LYPaintView *)paintView withPwd:(NSString *)pawd{
29     if ([pawd isEqualToString:@"345"]) {
30         return YES;
31     }
32     return NO;
33 }
34 @end
LYPaintView.h
//文件内容,主要是创建协议,设置代理属性
 1 #import <UIKit/UIKit.h>
 2 @class LYPaintView;
 3 //创建协议
 4 @protocol LYPaintViewDelegate <NSObject>
 5 //协议的方法
 6 -(BOOL)checkPwd:(LYPaintView *)paintView withPwd:(NSString *)pawd;
 7 
 8 @end
 9 
10 @interface LYPaintView : UIView
11 +(instancetype)paintView;
12 @property(nonatomic,weak)id <LYPaintViewDelegate>delegate;
13 @end
LYPaintView.m

//实现解锁

#import "LYPaintView.h"
#define CZLineColor [UIColor colorWithRed:0.0 green:170/255.0 blue:255/255.0 alpha:0.5]

@interface LYPaintView ()
@property(nonatomic,strong)NSMutableArray *btnsArray;

@property(nonatomic,strong)UIColor *lineColor;

@property(nonatomic,assign)CGPoint currentPoint;

@end

@implementation LYPaintView
//快速创建对象类方法
+(instancetype)paintView{
    return [[[NSBundle mainBundle]loadNibNamed:@"LYPaintView" owner:nil options:nil]lastObject];
    
}


//开始触摸事件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取触摸点
    UITouch *touch=touches.anyObject;
    CGPoint point=[touch locationInView:self];
    //遍历按钮看触摸点是否在按钮上
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        BOOL isContain=CGRectContainsPoint(obj.frame, point);
        if (isContain && obj.highlighted==NO) {
            obj.highlighted=YES;
            [self.btnsArray addObject:obj];
        }else{
            obj.highlighted =NO;
        }
        
    }];
    
}
//触摸移动时
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //获取当前的触摸点
    UITouch *touch=touches.anyObject;
    CGPoint point=[touch locationInView:self];
    //设置当前点
    self.currentPoint=point;
    //遍历按钮看触摸点是否在按钮上
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        BOOL isContain=CGRectContainsPoint(obj.frame, point);
        if (isContain && obj.highlighted==NO) {
            obj.highlighted=YES;
            [self.btnsArray addObject:obj];
            
        }
    }];
    //重绘
    [self setNeedsDisplay];
}
//触摸结束时
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //消除最后的线
   UIButton *lastBtn = self.btnsArray.lastObject;
    self.currentPoint=lastBtn.center;
    [self setNeedsDisplay];
    //遍历触摸过的按钮获得输入的密码
    NSMutableString *password=[NSMutableString string];
    [self.btnsArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger tag=obj.tag;
        [password appendFormat:@"%@",@(tag)];
    }];
    BOOL isTrue;
    //代理实现判断密码是否一致
    if ([self.delegate respondsToSelector:@selector(checkPwd:withPwd:)]) {
       isTrue = [self.delegate checkPwd:self withPwd:password];
    }
    if (isTrue) {
        NSLog(@"密码正确");
        //密码正确时恢复按钮的状态
        [self.btnsArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.highlighted=NO;
            
        }];
        [self.btnsArray removeAllObjects];
        [self setNeedsDisplay];
        
    }else{
        NSLog(@"密码错误");
         //密码错误时线变为红色
        self.userInteractionEnabled=NO;
        [self.btnsArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.highlighted=NO;
            obj.enabled=NO;
        }];
        self.lineColor=[UIColor redColor];
        [self setNeedsDisplay];
        //延迟1秒恢复按钮的原来状态
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.btnsArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                obj.enabled=YES;
            }];
            [self.btnsArray removeAllObjects];
            [self setNeedsDisplay];
            self.userInteractionEnabled=YES;
            self.lineColor=CZLineColor;
        });
        
       
        
    }
    
}
//加载按钮
-(void)awakeFromNib{
    self.lineColor=CZLineColor;
    
    for (int i=0; i<9; i++) {
        //创建按钮并添加
        UIButton *btn=[[UIButton alloc]init];
        btn.tag=i;
        btn.userInteractionEnabled = NO;
        
        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_error"] forState:UIControlStateDisabled];
        
        [self addSubview:btn];
    }
    
}
//设置按钮的frame
-(void)layoutSubviews{
    [super layoutSubviews];
    int conNum=3;
    CGFloat btnW=70;
    CGFloat btnH=70;
    CGFloat magin=(self.frame.size.width - conNum *btnW)/(conNum -1);
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSInteger col=idx % conNum;
        NSInteger row=idx / conNum;
        CGFloat btnX=col *(magin + btnW);
        CGFloat btnY=row *(magin + btnH);
        
        obj.frame=CGRectMake(btnX, btnY, btnW, btnH);

    }];
    
}
//drawRect方法绘制线条
- (void)drawRect:(CGRect)rect {
    //如果数组内没有元素就不要绘制了
    if (self.btnsArray.count==0) {
        return ;
    }
    
    //创建路径
    UIBezierPath *path=[UIBezierPath bezierPath];
    [self.btnsArray enumerateObjectsUsingBlock:^(UIButton *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx==0) {
            [path  moveToPoint:obj.center];
        }else{
            [path addLineToPoint:obj.center];
        }
    }];
    //添加最后的线段
    [path addLineToPoint:self.currentPoint];

    [self.lineColor setStroke];
    [path setLineWidth:10];
    [path setLineCapStyle:kCGLineCapRound];
    [path setLineJoinStyle:kCGLineJoinRound];
    //渲染
    [path stroke];
    
}
//懒加载按钮数组
-(NSMutableArray *)btnsArray{
    if (_btnsArray==nil) {
        _btnsArray=[NSMutableArray array];
    }
    return _btnsArray;
}



@end

 

以上是关于代码笔记手势解锁的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发之手势解锁

使用 rollup 打包一个原生 js + canvas 实现的移动端手势解锁功能组件

FQLockSDK: iOS手势密码解锁,面容ID解锁,指纹解锁

FQLockSDK: iOS手势密码解锁,面容ID解锁,指纹解锁

FQLockSDK: iOS手势密码解锁,面容ID解锁,指纹解锁

FQLockSDK: iOS手势密码解锁,面容ID解锁,指纹解锁