UIEvent をトリガーしたタッチ ジェスチャの種類を取得する方法はありますか? では、hitTest:withEvent: でジェスチャをインターセプトするとします。このメソッドをトリガーしたタッチ (ピンチ、タップ、スワイプなど) を特定するにはどうすればよいですか?
UIEvent サブタイプからリモート イベントのタイプを取得できます。デバイスが揺れても、タッチ イベントのパラメータはありません...
それらをどのように識別しますか?
自分でタッチ分析を行う必要があります。
それほど難しくはありませんが、あなたのために行われるわけではありません。ここで、私が実際に使用しているコードのサンプルを示します。これは、本格的なソリューションを意図したものではありません。非常に具体的なコンテキスト(私のアプリのコンテキスト)で基本的なタップ/スワイプ/ピンチジェスチャーのみを検出し、ジェスチャー(通知)を配信するためにかなりクールなメカニズムを使用します。したがって、それはあなたのケースに当てはまるか、当てはまらない可能性が高いかもしれませんが、何が必要かについてのアイデアを提供してくれることを願っています.
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");
}
タッチ(タッチの数とその位置)を自分で分析する必要があります。もう 1 つの簡単な方法は、対応するジェスチャ認識エンジンを追加して、対応するタイプを取得することです。すべてのタッチ イベントのイベント タイプはUIEventSubtypeNone
です。