【问题标题】:iOS Multi-Touch Not WorkingiOS 多点触控不起作用
【发布时间】:2013-07-26 17:46:57
【问题描述】:

我正在进行常规的 OpenGL / EAGL 设置:

@interface EAGLView : UIView {
@public
    EAGLContext* context;
}
@property (nonatomic, retain) EAGLContext* context;
@end

@implementation EAGLView
@synthesize context;
+ (Class)layerClass {
    return [CAEAGLLayer class];
}
@end

@interface EAGLViewController : UIViewController {
@public
    EAGLView* glView;
}
@property(nonatomic, retain) EAGLView* glView;
@end

@implementation EAGLViewController
@synthesize glView;

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    for (UITouch* touch in touches) {
        CGPoint location = [touch locationInView:glView];
        int index;
        for (index = 0; index < gCONST_CURSOR_COUNT; ++index) {
            if (sCursor[index] == NULL) {
                sCursor[index] = touch;
                break;
            }
        }
    }
    [super touchesBegan:touches withEvent:event];
}

该实现还包括相应的 touchesEnded/Canceled/Moved。该代码完全可以正常工作并且可以很好地跟踪。

我还确保我为所有内容提供了正确的值:

sViewController = [EAGLViewController alloc];

CGRect rect = [[UIScreen mainScreen] applicationFrame];
sViewController.glView = [[EAGLView alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
Assert(sViewController.glView);
sViewController.glView.userInteractionEnabled = YES;
sViewController.glView.multipleTouchEnabled = YES;
sViewController.glView.exclusiveTouch = YES;

这一切都编译得很好,但我从来没有收到超过一个 UITouch。我的意思不是在一次 touchesBegan 中,而是索引永远不会超过 0。我还为它第二次进入该函数设置了一个断点,并且将两根手指放在上面不会触发它。

【问题讨论】:

    标签: ios objective-c multi-touch uitouch eaglview


    【解决方案1】:

    如果您想检测多个触摸(和/或区分一根手指、两根手指等触摸),请尝试使用 UIPanGestureRecognizer。设置时,您可以指定最小和最大触摸次数。然后将其附加到要检测触摸的视图。当您从它接收事件时,您可以询问它接收到多少次触摸并相应地分支。

    这是苹果文档:

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UIPanGestureRecognizer_Class/Reference/Reference.html

    如果您这样做,您可能根本不需要使用 touchesBegan/Moved/Ended 方法,并且根据您设置手势识别器的方式,可能永远不会调用 touchesBegan/Moved/Ended。

    【讨论】:

    • 我真的必须使用 touchesBegan/Moved/Ended,因为我在较低级别(游戏引擎)处理手势/触摸。它目前在其他平台上运行。
    • 您是否也在检查 touchesMoved 中的多次触摸?
    • 是的。 touchesMoved 似乎也只带来了一次触摸。如果是这样,我的代码也会崩溃,因为它希望 touchesBegan 首先使用特定的 UITouch 触发。
    【解决方案2】:

    使用[event allTouches] 代替touchestouches 仅代表已“更改”的触摸。来自苹果文档:

    如果您对自上次以来未更改的触摸感兴趣 阶段或与触摸不同的阶段 传入的集合,你可以在事件对象中找到那些。图 3-2 描绘了一个包含触摸对象的事件对象。得到所有 这些触摸对象,调用事件对象的 allTouches 方法。

    【讨论】:

    • 不幸的是,这并不能解释为什么 touchesBegan 没有为我的第二根手指开火。
    【解决方案3】:

    看来我缺少的只是这个:

    sViewController.view = sViewController.glView;
    

    【讨论】:

    • 似乎视图没有直接与视图控制器相关联,多点触控的行为不正常。
    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 2015-06-30
    相关资源
    最近更新 更多