2013-02-06 64 views
7

有沒有辦法獲得觸發UIEvent的觸摸手勢類型?所以,假設我用hitTest:withEvent:截取手勢。如何識別觸發此方法的觸摸(捏,輕敲,滑動等)?從UIEvent對象中識別觸摸手勢的類型

我可以從的UIEvent亞型獲得遠程事件的類型,即使設備被動搖,但對於觸摸事件沒有參數...

如何識別那些?

+0

@blub:其實,這是沒有重複的 - 這裏的問題是關於則hitTest低水平的觸摸檢測:withEvent:方法 – sergio

+0

可能「是我的錯,對不起」 – blub

回答

5

你必須自己做觸摸分析。

這並不困難,但它不適合你。這裏是我實際使用的一些代碼的示例。它不是一個全面的解決方案。它只能在非常具體的上下文(我的應用程序的上下文)中檢測基本的輕拍/輕掃/捏手勢,並使用相當不舒服的機制來傳遞手勢(通知)。所以,它可能適用於你的情況,或者更可能不適用,但我希望它能讓你瞭解需要什麼。

NSSet* allTouches = [event allTouches]; 
UITouch* touch = [allTouches anyObject]; 
UIView* touchView = [touch view]; 

     if (touch.phase == UITouchPhaseBegan) { 

      _initialView = touchView; 
      startTouchPosition1 = [touch locationInView:self]; 
      startTouchTime = touch.timestamp; 

      if ([allTouches count] > 1) { 
       startTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 
       previousTouchPosition1 = startTouchPosition1; 
       previousTouchPosition2 = startTouchPosition2; 
      } 
     } 

     if (touch.phase == UITouchPhaseMoved) { 

      if ([allTouches count] > 1) { 
       CGPoint currentTouchPosition1 = [[[allTouches allObjects] objectAtIndex:0] locationInView:self]; 
       CGPoint currentTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self]; 

       CGFloat currentFingerDistance = CGPointDist(currentTouchPosition1, currentTouchPosition2); 
       CGFloat previousFingerDistance = CGPointDist(previousTouchPosition1, previousTouchPosition2); 
       if (fabs(currentFingerDistance - previousFingerDistance) > ZOOM_DRAG_MIN) { 
        NSNumber* movedDistance = [NSNumber numberWithFloat:currentFingerDistance - previousFingerDistance]; 
        if (currentFingerDistance > previousFingerDistance) { 
//       NSLog(@"zoom in"); 
         [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_IN object:movedDistance]; 
        } else { 
//       NSLog(@"zoom out"); 
         [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_OUT object:movedDistance]; 
        } 
       } 
      } 
     } 

     if (touch.phase == UITouchPhaseEnded) { 
      CGPoint currentTouchPosition = [touch locationInView:self]; 

      // Check if it's a swipe 
      if (fabsf(startTouchPosition1.x - currentTouchPosition.x) >= SWIPE_DRAG_HORIZ_MIN && 
       fabsf(startTouchPosition1.x - currentTouchPosition.x) > fabsf(startTouchPosition1.y - currentTouchPosition.y) && 
       touch.timestamp - startTouchTime < 0.7) 
      { 
       // It appears to be a swipe. 
       if (startTouchPosition1.x < currentTouchPosition.x) { 
         NSLog(@"swipe right"); 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_RIGHT object:self]; 
       } else { 
         NSLog(@"swipe left"); 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_LEFT object:self]; 
       } 
      } else { 
       //-- else, check if it's a single touch 
       if (touch.tapCount == 1) { 
        NSDictionary* uInfo = [NSDictionary dictionaryWithObject:touch forKey:@"touch"]; 
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_TAP object:self userInfo:uInfo];       
       }/* else if (touch.tapCount > 1) { 
        handle multi-touch 
       } 
       */ 
      } 

      startTouchPosition1 = CGPointMake(-1, -1); 
      _initialView = nil; 
     } 

     if (touch.phase == UITouchPhaseCancelled) { 
      _initialView = nil; 
//   NSLog(@"TOUCH CANCEL"); 
     } 
+0

我給它一個測試運行和報告回來一會兒。謝謝! – user1799795

+0

請考慮我的代碼只是一個片段:它不是一個全面的解決方案。它只能在非常具體的上下文(我的應用程序的上下文)中檢測基本的輕拍/輕掃/捏手勢,並使用相當不舒服的機制來傳遞手勢(通知)。我希望它能讓你瞭解需要什麼。 – sergio

+0

我意識到這一點。 :) – user1799795

0

您需要自己分析觸摸(觸摸次數和位置)。另一個更簡單的方法是添加相應的手勢識別器以獲取相應的類型。所有觸摸事件的事件類型爲UIEventSubtypeNone