Iphone 从UIEvent对象识别触摸手势的类型

Iphone 从UIEvent对象识别触摸手势的类型,iphone,ios,hittest,uievent,Iphone,Ios,Hittest,Uievent,有没有办法获得触发UIEvent的触摸手势类型?假设我截获了一个带有hitTest:withEvent:的手势。我如何识别触发此方法的触摸类型(捏、敲、刷等) 我可以从UIEvent子类型中获得远程事件的类型,即使设备受到震动,但没有触摸事件的参数 如何识别这些?您需要自己分析触摸(触摸次数和位置)。另一个更简单的方法是添加相应的手势识别器以获得相应的类型。所有触摸事件的事件类型都是uieventsubsubtynone您必须自己进行触摸分析 这并不难,但这不是为你做的。这里是我实际使用的一些代

有没有办法获得触发UIEvent的触摸手势类型?假设我截获了一个带有hitTest:withEvent:的手势。我如何识别触发此方法的触摸类型(捏、敲、刷等)

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


如何识别这些?您需要自己分析触摸(触摸次数和位置)。另一个更简单的方法是添加相应的手势识别器以获得相应的类型。所有触摸事件的事件类型都是
uieventsubsubtynone

您必须自己进行触摸分析

这并不难,但这不是为你做的。这里是我实际使用的一些代码的示例。这并不是一个全面的解决方案。它只在一个非常具体的上下文(我的应用程序的上下文)中检测基本的点击/滑动/挤压手势,并使用一种相当不酷的机制来传递手势(通知)。因此,它可能适用于您的情况,或者更可能不适用于您的情况,但我希望它能让您了解需要什么

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");
        }
NSSet*alltouchs=[event alltouchs];
UITouch*touch=[AllTouchs anyObject];
UIView*touchView=[触摸视图];
如果(touch.phase==UITouchPhaseBegan){
_初始视图=触摸视图;
开始触摸位置1=[触摸位置查看:自];
startTouchTime=touch.timestamp;
如果([AllTouchs计数]>1){
startTouchPosition2=[[AllTouchs AllObject]对象索引:1]位置查看:自];
previousTouchPosition1=开始触摸位置1;
previousTouchPosition2=开始触摸位置2;
}
}
如果(touch.phase==UITouchPhaseMoved){
如果([AllTouchs计数]>1){
CGPoint currentTouchPosition1=[[AllTouchs AllObject]对象索引:0]位置查看:自];
CGPoint currentTouchPosition2=[[AllTouchs AllObject]对象索引:1]位置查看:自];
CGFloat currentFingerDistance=CGPointDist(currentTouchPosition1,currentTouchPosition2);
CGFloat previousFingerDistance=CGPointDist(previousTouchPosition1,previousTouchPosition2);
if(fabs(当前手指距离-上一个手指距离)>缩放\u拖动\u最小值){
NSNumber*MovedInstance=[NSNumber numberWithFloat:currentFingerDistance-previousFingerDistance];
如果(currentFingerDistance>previousFingerDistance){
//NSLog(@“放大”);
[[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION\u ZOOM\u IN object:MovedInstance];
}否则{
//NSLog(“缩小”);
[[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION\u ZOOM\u OUT object:MovedInstance];
}
}
}
}
如果(touch.phase==UITouchPhaseEnded){
CGPoint currentTouchPosition=[触摸位置查看:自];
//检查是否是刷卡
如果(fabsf(startTouchPosition1.x-currentTouchPosition.x)>=滑动拖动水平最小值&&
fabsf(startTouchPosition1.x-currentTouchPosition.x)>fabsf(startTouchPosition1.y-currentTouchPosition.y)&&
touch.timestamp-开始触摸时间<0.7)
{
//这似乎是一次重击。
如果(开始接触位置1.x<当前接触位置.x){
NSLog(@“向右滑动”);
[[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION\u SWIPE\u RIGHT object:self];
}否则{
NSLog(@“向左滑动”);
[[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION\u SWIPE\u LEFT object:self];
}
}否则{
//--否则,请检查是否是单触
如果(touch.tapCount==1){
NSDictionary*uInfo=[NSDictionary Dictionary WithObject:touch-forKey:@“touch”];
[[NSNotificationCenter defaultCenter]postNotificationName:NOTIFICATION\u TAP object:self-userInfo:uInfo];
}/*否则如果(touch.tapCount>1){
多点触摸手柄
}
*/
}
startTouchPosition1=CGPointMake(-1,-1);
_初始视图=零;
}
如果(touch.phase==UITouchPhase取消){
_初始视图=零;
//NSLog(“触摸取消”);
}

自从iOS 7以来,有一种委托方法可以让您了解名为
textView的URL交互(u:shouldInteractWith:in:interaction:)


@blub:事实上,它不是重复的——这里的问题是关于hitTest中的低级别触摸检测:withEvent:可能是“我的错,对不起”,我会对它进行一次测试,稍后再报告。谢谢请考虑我的代码只是一个片段:它不意味着是一个完整的解决方案。它只在一个非常具体的上下文(我的应用程序的上下文)中检测基本的点击/滑动/挤压手势,并使用一种相当不酷的机制来传递手势(通知)。我希望它能让你知道需要什么。我已经试过你的解决方案了。问题是它不能与hitTest:withEvent:一起工作,因为[event alltouchs]返回一个空对象。尽管如此,如果我将您的代码放入一个常规的touchestart:withevent:method中,它仍然有效。实际上,我正在自定义
ui窗口中使用该代码,并且在该窗口中定义了触摸。我不知道你想做什么,但是你可以自定义你的UIWindow并在那里进行手势检测吗?我会仔细考虑。。。无论如何谢谢你!如果没有其他人回答我