手势解锁demo


#import "LSbackView.h"

@interface LSbackView()

@property(nonatomic,strong)NSMutableArray *btnArray;

@property(nonatomic,strong)NSMutableArray <UIButton *>*selectionArray;

@property(nonatomic,assign)CGPoint lastPoint;

@property(nonatomic,strong)UIColor *lineColor;

@end

@implementation LSbackView

-(UIColor *)lineColor{

    if (!_lineColor) {

        _lineColor = [UIColor whiteColor];

        

    }

    return _lineColor;

}

-(NSMutableArray *)selectionArray{

    if (!_selectionArray) {

        _selectionArray = [NSMutableArray array];

    }

    return _selectionArray;

}

-(NSMutableArray *)btnArray{

    if (!_btnArray) {

        _btnArray = [NSMutableArray array];

        for (NSInteger i=0; i<9; i++) {

            UIButton *button = [[UIButton alloc]init];

            button.tag = i;

            NSLog(@"%ld",i);

            button.userInteractionEnabled = NO;

            [button setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];

            [button setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateHighlighted];

            

            [button setBackgroundImage:[UIImage imageNamed:@"gesture_node_error"] forState:UIControlStateSelected];

            [_btnArray addObject:button];

            

        }

    }

    return _btnArray;

}

-(instancetype)initWithFrame:(CGRect)frame{

    if (self=[super initWithFrame:frame]) {

      

        

        CGFloat W = self.frame.size.width;

        CGFloat H = self.frame.size.height;

        

        CGFloat btnW = 74;

        CGFloat btnH = 74;

        

        NSInteger column = 3;

        CGFloat marginW = (W-column*btnW)/(column-1);

        CGFloat marginH = (H-column*btnH)/(column-1);

        

        for (NSInteger i=0; i<self.btnArray.count; i++) {

            NSInteger row = i/column;

            NSInteger col = i%column;

            CGFloat btnX = (btnW + marginW)*col;

            CGFloat btnY = (btnH + marginH)*row;

            

            UIButton *btn = self.btnArray[i];

            

            btn.frame = CGRectMake(btnX, btnY, btnW, btnH);

            [self addSubview:btn];

        }

        

    }

    return self;

}



-(void)drawRect:(CGRect)rect{

    UIBezierPath *path = [UIBezierPath bezierPath];

    for (NSInteger i=0;i<self.selectionArray.count; i++) {

        if (i==0) {

            [path moveToPoint:self.selectionArray[i].center];

        }else{

            [path addLineToPoint:self.selectionArray[i].center];

        }

    }

    

    if (self.selectionArray.count>0) {

        [path addLineToPoint:self.lastPoint];

    }

    

    [self.lineColor set];

    [path stroke];

    

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

//    UITouch *touch = touches.anyObject;

//    CGPoint loc = [touch locationInView:touch.view];

//    for (UIButton *btn in self.btnArray) {

//        if (CGRectContainsPoint(btn.frame, loc)) {

//            btn.highlighted = YES;

//        }

//    }

    for (UITouch *touch in touches) {

        CGPoint loc = [touch locationInView:touch.view];

        self.lastPoint = loc;

        for (UIButton *btn in self.btnArray) {

            if (CGRectContainsPoint(btn.frame, loc)&&!btn.highlighted) {

                

                btn.highlighted = YES;

                [_selectionArray addObject:btn];

            }

        }

    }

     [self setNeedsDisplay];

    

}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    UITouch *touch = touches.anyObject;

    CGPoint loc = [touch locationInView:touch.view];

    self.lastPoint = loc;

    for (UIButton *btn in self.btnArray) {

        if (CGRectContainsPoint(btn.frame, loc)&&!btn.highlighted) {

        

            btn.highlighted = YES;

            [_selectionArray addObject:btn];

        }

    }

     [self setNeedsDisplay];

}

//-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

//    

//}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    self.lastPoint = [self.selectionArray lastObject].center;

  

    NSMutableString *str = [NSMutableString string];

    for (UIButton *button in self.selectionArray) {

        [str appendFormat:@"%@",@(button.tag)];

    }

    if ([str isEqualToString:@"012"]) {

        [self clear];

    }else{

        self.lineColor = [UIColor redColor];

        [self setNeedsDisplay];

        self.userInteractionEnabled = NO;

        for (UIButton *button in self.selectionArray) {

            button.highlighted = NO;

            button.selected = YES;

        }

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [self clear];

            self.userInteractionEnabled = YES;

            

        });

    }

    

}

-(void)clear{

    for (UIButton *button in self.selectionArray) {

        button.highlighted = NO;

        button.selected = NO;

        self.lineColor = [UIColor whiteColor];

    }

    [self.selectionArray removeAllObjects];

    [self setNeedsDisplay];

}

@end




最容易出坑的地方

CGRectContainPoint方法


还有在drawRect方法里添加线段的方法


设置数组添加线段的方法



最后血淋淋呀,还有添加选择按钮进数组的时候,如果没有!btn.highlighted这个条件,密码始终是打印一堆,人都要疯了


添加线段注意写在括号的里外问题,注意呀  注意

你可以参考以下步骤实现OC手势解锁demo: 1. 创建一个手势解锁的视图,可以使用`UIView`或`UIScrollView`等。 2. 在手势解锁视图中添加手势识别器,可以使用`UIPanGestureRecognizer`。 3. 在手势识别器回调函数中,记录手指的移动轨迹,可以使用`CGPoint`类型的数组。 4. 根据手指移动轨迹,在手势解锁视图上绘制手势轨迹,可以使用`UIBezierPath`绘制。 5. 根据手势轨迹判断手势是否正确,可以使用事先定义好的手势密码进行比对。 6. 根据比对结果,显示相应的提示信息。 以下是一个简单的OC手势解锁demo示例代码: ``` // 创建手势解锁视图 UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; gestureView.center = self.view.center; gestureView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:gestureView]; // 添加手势识别器 UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [gestureView addGestureRecognizer:gestureRecognizer]; // 定义手势密码 NSArray *password = @[@(1), @(2), @(3), @(6), @(9)]; // 记录手指移动轨迹 NSMutableArray *path = [NSMutableArray array]; // 绘制手势轨迹 - (void)drawPath:(NSArray *)path { UIBezierPath *bezierPath = [UIBezierPath bezierPath]; for (int i = 0; i < path.count; i++) { CGPoint point = [path[i] CGPointValue]; if (i == 0) { [bezierPath moveToPoint:point]; } else { [bezierPath addLineToPoint:point]; } } CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.path = bezierPath.CGPath; shapeLayer.strokeColor = [UIColor redColor].CGColor; shapeLayer.fillColor = [UIColor clearColor].CGColor; [self.view.layer addSublayer:shapeLayer]; } // 判断手势是否正确 - (BOOL)checkPassword:(NSArray *)path { if (path.count != password.count) { return NO; } for (int i = 0; i < path.count; i++) { if ([path[i] integerValue] != [password[i] integerValue]) { return NO; } } return YES; } // 手势识别器回调函数 - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer { CGPoint location = [gestureRecognizer locationInView:gestureView]; if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { [path removeAllObjects]; [path addObject:[NSValue valueWithCGPoint:location]]; } else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { [path addObject:[NSValue valueWithCGPoint:location]]; [self drawPath:path]; } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { if ([self checkPassword:path]) { NSLog(@"密码正确"); } else { NSLog(@"密码错误"); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值