iPhone:检测两指触摸

标签 iphone objective-c

我需要检测两个手指触摸事件。如果我同时用两个手指触摸屏幕,那么一切正常。只需使用这样的代码:

- (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/

相关文章:

objective-c - 如何在 Objective-C 中随事件发送和接收数据?

ios - Objective-C 创建一个 ViewController 的实例

objective-c - UISegmentedControl 自定义背景图片

iphone - 在 Iphone 的日期选择器中插入空白值

ios - 如何将手机的所有远程(推送)通知重新安排到特定时间?在iOS中有可能吗?

ios - 自动展开 UITableView - NSRangeException

iphone - GCC 4.2.1 darwin 避免重复符号

javascript - 为谷歌搜索结果触发 webViewDidFinishLoad

iphone - 识别我的 UILongPressGestureRecognizer 是从哪个单元格按下的,无法向其添加标签

ios - UITextField secureTextEntry 带有自定义字体的项目符号?