【问题标题】:How to make OpenGL view and overlaying UIView react to the same touch event如何使 OpenGL 视图和覆盖 UIView 对相同的触摸事件做出反应
【发布时间】:2014-05-21 03:41:24
【问题描述】:

我有一个带有精灵的 OpenGL 视图,其中的 UIView 在 OpenGL 视图的顶部具有相同的位置和大小,以使它们可以访问。 OpenGL 视图应该能够接收触摸事件和顶部的 UIView。在我的 UIView 类中,我重写了 hitTest:withEvent 方法,以便 UIViews 获得触摸输入,否则只有 OpenGL 获得触摸:

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSEnumerator *reverseE = [self.subviews reverseObjectEnumerator];
    UIView *iSubView;
    while ((iSubView = [reverseE nextObject]))
    {
        UIView *viewWasHit = [iSubView hitTest:[self convertPoint:point toView:iSubView] withEvent:event];
        if (viewWasHit)
        {
            return viewWasHit;
        }
    }
    return [super hitTest:point withEvent:event];
}

现在我的 UIViews 接收触摸输入,但下面的 OpenGL 元素没有。我怎样才能改变它,让他们都收到触摸。

【问题讨论】:

    标签: objective-c uiview opengl-es


    【解决方案1】:

    在我看来,在这种情况下,最好只有您的主视图接收到触摸,然后使用 CGRectContainsPoint 检查是否有任何子视图被触摸。如果你真的想覆盖子视图,那么我建议你覆盖触摸方法来调用父视图触摸方法,例如覆盖touchesBegin

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [self.superview touchesBegan:touches withEvent:event];
        //do additional stuff
    }
    

    这适用于触摸,但可能不适用于手势,因为它们会取消触摸事件并具有不同的管道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多