首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从UIEvent对象识别触摸手势的类型

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

Stack Overflow用户
提问于 2013-02-06 17:55:58
回答 3查看 8.1K关注 0票数 10

有没有办法获得触发UIEvent的触摸手势类型?因此,假设我使用hitTest: with hitTest:截取了一个手势。如何识别哪种触摸(按压、轻敲、轻扫等)触发此方法的原因是什么?

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

你如何识别这些?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-06 18:16:39

你必须自己做触摸分析。

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

代码语言:javascript
复制
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");
        }
票数 8
EN

Stack Overflow用户

发布于 2013-02-06 18:04:17

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

票数 0
EN

Stack Overflow用户

发布于 2020-07-31 23:08:00

从iOS 7开始,就有一个叫做textView(_:shouldInteractWith:in:interaction:)的委托方法让你知道网址的交互。

https://developer.apple.com/documentation/uikit/uitextviewdelegate/1649337-textview

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14726087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档