2012-01-08 37 views
4

是否有公共或私有API让应用程序获取被触摸的表面?我对Android和/或iOS感兴趣。检测iphone或android上的触摸形状?

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

解决方案将返回一个矩形的矩形触摸也可以。

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

+0

我不知道iOS上的任何内容。 iOS会检测手指本身的位置,我不认为您可以访问原始传感器信息。 – fishinear 2012-01-08 19:14:22

+0

除非有人找到方法,否则我认为这不适用于Android。 Android注册触摸单像素坐标,我认为你必须绕过操作系统,并达到硬件水平。 – DeeV 2012-01-23 18:12:36

+1

在Android上,我应该认为最好的方法是使用MotionEvent.getSize(),它返回手指的大小(不是形状)。 http://developer.android.com/reference/android/view/MotionEvent.html#getSize%28int%29 – Jave 2012-01-24 09:01:41

回答

3

根据要求答案:
在Android上,我认为最好的办法是使用MotionEvent.getSize(),它返回手指的大小(不是形状)。 API演示TouchPaint使用它。

+0

感谢您的帮助,Jave!我在下面的单独答案中发布了一个完整的解决方案(iPhone)。 – johndodo 2012-01-30 08:18:06

1

这段代码是如何获得感动矩形的想法。对于每触摸一个新的矩形,计数器将被添加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. 
} 

它必须放置在视图控制器,你要检测的触摸形状,其观点上。

+0

如果我理解正确,只有触摸的中心被iOS报告?所以这段代码跟随手指的中心并将其路径(而不是形状)保存在矩阵中? – johndodo 2012-01-24 06:47:53

+0

就是这样。在iOS中,触摸识别器仅通过触摸发送X和Y.如果你想跟踪两个手指,你唯一需要做的就是用手指和矩阵与触摸之间的关系。 – Esepakuto 2012-01-24 08:38:51

+0

好的,所以如果我想要一个**单**手指的形状,当**没有**移动,我运气不好?顺便说一句,即使手指覆盖的区域的大小会有所帮助... Android似乎支持这一点(请参阅我的答案),iOS如何? – johndodo 2012-01-24 09:00:54

1

我已找到适用于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方法,我将不胜感激。

+0

好的研究!请告诉我它是否有效 – Esepakuto 2012-01-24 11:19:19