从UIEvent对象识别触摸手势的types

有没有办法获得触发一个UIEvent触摸手势的types? 所以,假设我用hitTest:withEvent:截取了一个手势。 如何识别触发此方法的触摸(捏,轻敲,滑动等)?

我可以从UIEvent子types获得远程事件的types,即使设备被震动,但没有触摸事件的参数…

你如何识别这些?

你必须自己做触摸分析。

这并不难,但这不是为你做的。 这里是我实际使用的一些代码的示例。 这不是一个全面的解决scheme。 它只检测一个非常具体的背景(我的应用程序)的基本轻拍/轻扫/捏手势,并使用一个相当uncool机制来提供手势(通知)。 所以,它可能适用于你的情况,或者更可能不适用,但是我希望它能让你了解需要什么。

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"); } 

您需要自己分析触摸(触摸次数和位置)。 另一个更简单的方法是添加相应的手势识别器来获得相应的types。 所有触摸事件的事件types都是UIEventSubtypeNone