2013-02-06 12 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です。

関連する問題