如何检测鼠标/手指是否沿着特定路径移动?

发布于 2024-07-15 06:02:41 字数 118 浏览 6 评论 0原文

我正在为 iPhone 开发一个应用程序,我想检测用户何时沿着特定路径移动手指。

具体来说,我想检测用户何时沿着特定的圆形路径移动手指。

这可能很容易通过检测鼠标沿路径的运动来适应。

I'm developing an application for iPhone, and I want to detect when the user is moving his finger along a certain path.

Specifically, I want to detect when the user moves their finger along a specific circular path.

This would probably be easily adapted from detecting mouse motion along a path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-07-22 06:02:41

似乎特定案例的实施起来相当简单。 将输入区域分为四个象限并跟踪每个象限的位置。 象限的顺序决定了它们是否已经完成或绕了一圈。

描述这个想法的伪代码:

static touchKeeper; //touch, quadrant
static touchIndex=0;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
   touchKeeper[touchIndex].touch =  touch;
   touchKepper[touchIndex].quadrant = DetectQuadrant(touch));
   if (touchIndex > 3 && (touchKeeper[3].quadrant == touchKeeper[2].quadrant+1 == touchKeeper[1].quadrant+2 == touchKeeper[0].quadrant+3)) {
     [self doCircleAction];
     touchIndex=0;
   } 
     else touchIndex++;
 }

这个想法是你只需要粗略地识别一个区域的特定触摸。 所以象限例程会是这样的:

int DetectQuadrant(UITouch *touch) {
 CGPoint point;
 point = [touch locationInView:self];
 if (endPoint.x > X1 && endPoint.x < X2 && endPoint.Y > Y1 && endPoint.Y < Y2)
  return QUAD1;
 } else etc

我使用象限作为例子 - 显然你可以画一个正方形并认为“圆形!”。 通过在 Detect 方法中创建更多区域来提高准确性。 主要思想是根据区域序列进行思考,以便实现所需的行为,而不会过度复杂化(如贝塞尔曲线检测或类似的废话)。 这个伪代码肯定是粗暴的,我确信有一个很好的方法来巧妙地实现。 我一直在为我正在从事的一个项目考虑“圆周运动检测”,这是我想出的粗略想法。

享受!

Seems like a specific case can be fairly simple to implement. Divide the input area into four quadrants and track the position through each quadrant. The sequence of quandrants determines if they have completed or traveled in a circle.

Pseudo-code to describe the idea:

static touchKeeper; //touch, quadrant
static touchIndex=0;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
   touchKeeper[touchIndex].touch =  touch;
   touchKepper[touchIndex].quadrant = DetectQuadrant(touch));
   if (touchIndex > 3 && (touchKeeper[3].quadrant == touchKeeper[2].quadrant+1 == touchKeeper[1].quadrant+2 == touchKeeper[0].quadrant+3)) {
     [self doCircleAction];
     touchIndex=0;
   } 
     else touchIndex++;
 }

The idea is that you only need to roughly identify a particular touch to a region. So the quadrant routine would something like this:

int DetectQuadrant(UITouch *touch) {
 CGPoint point;
 point = [touch locationInView:self];
 if (endPoint.x > X1 && endPoint.x < X2 && endPoint.Y > Y1 && endPoint.Y < Y2)
  return QUAD1;
 } else etc

I use quadrants as an example - clearly you could draw a square and think 'circle!'. The accuracy is improved by making more regions in the Detect method. The main idea is to think in terms of sequences of regions in order to achieve the desired behavior without excessive complication (like bezier curve detection or similar nonsense). This pseudo code is certainly brutish and I'm sure there's a nice way to finesse the implementation. I've been thinking about 'circular motion detection' for a project I'm working on and this was the rough idea I came up with.

Enjoy!

呆橘 2024-07-22 06:02:41

由于我对 iPhone SDK 本身不熟悉,所以我不确定是否有内置的用于捕获非标准手势的工具。 但是,这是您可以执行此操作的手动方法之一。

本教程适用于 Flash,但其操作原理相同。 每个方向代表一个数字。 使用定点设备画一个圆圈将生成一系列数字,代表随时间或距离移动的方向,这取决于您。 将该数字序列与表示圆的预定义序列进行比较。 如果它们匹配,则触发一个事件。

有很多教程解释了如何制作这样的“模糊”手势,以便需要不太精确的点运动。

Since I'm not familiar with the iPhone SDK itself, I'm not sure if there are built in facilities for capturing non-standard gestures. However, here is one of the manual ways you can do it.

This tutorial is for Flash, but it operates on the same gesture principles. Each direction represents a number. Tracing a circle with the pointing device will generate a sequence of numbers representing the directions travelled either over time or over distance, its up to you. Compare that sequence of numbers to a pre-defined sequence representing a circle. If they match, fire an event.

There are plenty of tutorials that explain how to make gestures like this "fuzzy" so that less precise point motion is required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文