UIPanGestureRecognizer 手势中 velocityInView、locationInView、translationInView

translationInView : 手指在视图上移动的位置(x,y)向下和向右为正,向上和向左为负。

locationInView : 手指在视图上的位置(x,y)就是手指在视图本身坐标系的位置。

velocityInView: 手指在视图上移动的速度(x,y), 正负也是代表方向,值得一体的是在绝对值上|x| > |y| 水平移动, |y|>|x| 竖直移动。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以参考以下步骤实现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、付费专栏及课程。

余额充值