2011-12-02 41 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框架的對象。這個框架對他們的對象做了奇怪的事情,因此觸摸以錯誤的順序返回。

我創建了一個空的項目來接收觸摸,一切都非常好。