从UIEvent对象中识别触摸手势的类型 [英] Identifying the Type of Touch Gestures from UIEvent Object

查看:951
本文介绍了从UIEvent对象中识别触摸手势的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to gain the type of touch gestures that triggered a UIEvent? So, let's say I intercept a gesture with hitTest:withEvent:. How do I identify what kind of touch (pinch, tap, swipe, etc.) happened that triggered this method?

我可以从UIEvent获取远程事件的类型子类型,即使设备被震动,但触摸事件没有参数......

I can gain the type of remote events from the UIEvent subtype, and even if the device was shaken, but there is no parameter for touch events...

你如何识别它们?

推荐答案

你必须自己进行触摸分析。

You have to do touch analysis on your own.

这并不困难,但它不适合你。这里是我实际使用的一些代码的示例。它不是一个全面的解决方案。它只在非常具体的环境(我的应用程序)中检测基本的点击/滑动/捏合手势,并使用相当不冷却的机制来传递手势(通知)。因此,它可能适用于您的情况或更可能不适用,但我希望它能让您了解所需的内容。

It is not that difficult, but it is not done for you. Here a sample from some code I am actually using. It is not meant as a full-scale solution. It only detects basic tap/swipe/pinch gestures in a very concrete context (that of my app), and uses a rather uncool mechanism to deliver the gestures (notifications). So, it might apply to your case or more likely not, but I hope it gives you an idea of what is required.

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

这篇关于从UIEvent对象中识别触摸手势的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆