我需要检测两个手指触摸事件。如果我同时用两个手指触摸屏幕,那么一切正常。只需使用这样的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
CGPoint point1 = [touch locationInView:self];
CGPoint point2 ;
NSLog(@"First: %f %f", point1.x, point1.y) ;
if ([[touches allObjects] count] > 1) {
UITouch *touch2 = [[touches allObjects] objectAtIndex:1];
point2 = [touch2 locationInView:self];
NSLog(@"Second: %f %f", point2.x, point2.y) ;
}
}
但是如果我用一根手指然后用另一根手指触摸屏幕,这段代码就不起作用了。如何实现?做起来难吗?
最佳答案
确保 UIView 有 multipleTouchEnabled=YES
,默认为 NO
。
编辑:我明白问题出在哪里了。在 touchesBegan:withEvent: 中,你只会得到新的触摸。您不会获得所有主动接触。如果可能的话,您不太可能同时开始一次以上的触摸。要检查是否有多个事件触摸 int touchesBegan:试试这个:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([[event touchesForView:self] count] > 1) {
NSLog(@"%d active touches",[[event touchesForView:self.view] count]) ;
}
[super touchesBegan:touches withEvent:event] ;
}
关于iPhone:检测两指触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4891135/