【问题标题】:detect touch shape on iphone or android?检测 iphone 或 android 上的触摸形状?
【发布时间】:2012-01-08 14:23:55
【问题描述】:

是否有公共或私有 API 可以让应用程序获得所接触的表面?我对 Android 和/或 iOS 感兴趣。

换句话说,我对触摸屏幕的手指的形状感兴趣。

返回触摸矩形矩阵的解决方案也可以。

(我 90% 肯定没有这样的事情,但我希望有人能证明我错了)

【问题讨论】:

  • 我不知道 iOS 上有类似的东西。 iOS 会自行检测您的手指在哪里,我认为您无法访问原始传感器信息。
  • 除非有人找到方法,否则我认为这不适用于 Android。 Android 在单像素坐标中注册触摸,我认为您必须绕过操作系统并进入硬件级别。
  • 在 Android 上,我认为您能做的最好的事情就是使用 MotionEvent.getSize(),它返回手指的大小(虽然不是形状)。 developer.android.com/reference/android/view/…
  • @Deev,fishinear:在两个平台上都可以,请参阅我的答案...但我仍然想找到其他方法,hack,在什么条件下可以完成的附加信息...我会奖励任何提供更多额外信息的人。
  • @Jave:是否愿意将其作为单独的答案发布?

标签: android ios touch


【解决方案1】:

根据要求回答:
在 Android 上,我应该认为您能做的最好的事情就是使用MotionEvent.getSize(),它返回手指的大小(虽然不是形状)。 API 演示 TouchPaint 使用了这个。

【讨论】:

  • 感谢您的帮助,杰夫!我在下面的单独答案中发布了一个完整的解决方案(iPhone 也是)。
【解决方案2】:

这段代码是关于如何获取被触摸的矩形的想法。对于每个触摸的新矩形,计数器将加 1,因此更容易跟踪触摸移动(如果需要)。为了更准确,更改常量 ROWS 和 COLS 的值

#define ROWS 15
#define COLS ROWS

int matrix[ROWS][COLS];
int squareCounter;


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];

    int col = floor((location.x * COLS) / self.view.frame.size.width);
    int row = floor((location.y * ROWS) / self.view.frame.size.height);
    if (matrix[col][row] == 0) matrix [col][row] = ++ squareCounter;

    [label setText:[NSString stringWithFormat:@"[%d][%d] -> %d", col, row, matrix[col][row]]];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSString *matrixStr = @"";
    for (int i = 0; i < COLS; i++) {
        for (int j = 0; j < ROWS; j++) {
            matrixStr = [matrixStr stringByAppendingFormat:@"%d\t", matrix[j][i]];
        }
        matrixStr = [matrixStr stringByAppendingString:@"\n"];
    }

    NSLog(@"Matrix:\n%@", matrixStr);
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    squareCounter = 0;
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            matrix[i][j] = 0;
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

它必须放在要检测触摸形状的视图控制器上。

【讨论】:

  • 如果我理解正确,iOS只报告触摸的中心?那么这段代码跟随手指的中心并将其路径(而不是形状)保存在矩阵中?
  • 就是这样。在 iOS 中,触摸识别器仅通过触摸发送 X 和 Y。如果您想跟踪两个手指,您唯一需要做的就是一个手指一个矩阵以及该矩阵与触摸之间的关系
  • 好的,所以如果我想要移动时手指的形状,我不走运吗?顺便说一句,即使是手指覆盖的区域大小也会有所帮助......Android似乎支持这一点(见我的回答),iOS怎么样?
  • 我认为这是与屏幕技术有关的问题。电阻屏可以提供类似于按下点矩阵的东西。但电容屏以其他方式工作,我严重怀疑您能否获得近似形状,因为定义形状需要大量点。
  • 触摸区域的近似大小对我来说就足够了 - 这在 iOS 上可能吗?请注意,Android 似乎支持这一点,所以我想这应该是可能的(硬件方面)。
【解决方案3】:

我找到了适用于 Android 的解决方案: https://stackoverflow.com/questions/8977510/motionevent-pointercoords-gettouchmajor-device-support。不过,我仍然需要了解它的支持程度……

对于 iPhone: Is there any way at all that I can tell how hard the screen is being pressed(见接受的答案)。 但是,似乎没有官方方式。

不过,如果有人添加一些官方或非官方/hackish 方法,我将不胜感激。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多