2011-12-02 38 views
1

我已经subclassed已经处理单触和拖动的UIView。我想增强此视图的交互,以便在拖动时,如果用户用第二根手指(视图中的任何其他位置)触摸,则系统会打印一条消息。我做的代码刺:复杂的iOS多点触控交互 - 有什么建议吗?

在我的头文件我已经声明:

NSString *key; // This unique key identifies the first touch 

我.m文件我有:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    for (UITouch *t in touches) { 
     if (key == NULL) 
     { 
      key = [[[NSValue valueWithPointer:t] description] copy]; 
     } 
     if ([key isEqualToString:[[NSValue valueWithPointer:t] description]]) 
     { 
      NSLog(@"calling parent to handle single touch"); 
      [super touchesBegan:[NSSet setWithObject:t] withEvent:event]; 
     } 
     else 
     { 
      [self twoTouchDetected]; 
     } 
    } 
} 

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *t in touches) { 
     if ([key isEqualToString:[[NSValue valueWithPointer:t] description]]) 
     { 
      [super touchesMoved:[NSSet setWithObject:t] withEvent:event]; 
     } 
    } 
} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *t in touches) { 
     if ([key isEqualToString:[[NSValue valueWithPointer:t] description]]) 
     { 
      [super touchesEnded:[NSSet setWithObject:t] withEvent:event]; 
      key = NULL; 
     } 
    } 
} 

不幸的是这个问题实现。第一次(当用一根手指拖动时)如果我用第二根手指触摸,系统将立即注册它。但是,第二次用第二根手指触摸(仍然继续用第一根手指拖动时),第二根手指触摸不会注册,直到第一根手指抬起。来自第二根手指的事件被备份...

也有一点奇怪的是,有时,父母被从第二手指而不是第一手触摸数据调用。

+0

为什么不使用[GestureRecognizers](http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html)? – beryllium

+0

我尝试过使用两个手指,一个点击GestureRecognizer,但如果我用手指拖动,那将无法工作。在传递给touchesBegin方法之前,识别器会消除所有事件。 – Joe

+0

拖动你需要使用UIPanGestureRecognizer – beryllium

回答

0

事实证明我的代码工作,但问题是我subclassed属于Core Plot框架的对象。这个框架对他们的对象做了奇怪的事情,因此触摸以错误的顺序返回。

我创建了一个空的项目来接收触摸,一切都非常好。