0

我有BooksEAGLView并且它有UIButton用于接收触摸事件。然后我的增强现实叠加的目的是添加叠加视图,BooksEAGLView然后我的按钮没有接收到触摸事件。

我怎样才能获得两个视图的触摸事件。

bookOverlayController = [[BooksOverlayViewController alloc]initWithDelegate:self];

 // Create the EAGLView
 eaglView = [[BooksEAGLView alloc] initWithFrame:viewFrame delegate:self appSession:vapp];
 [eaglView addSubview:bookOverlayController.view];
 [self setView:eaglView];

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
 return ([touch.view.superview isKindOfClass:[BooksEAGLView class]] || [touch.view.superview isKindOfClass:[TargetOverlayView class]]);
 }

触摸事件:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    NSLog(@"hitTest:withEvent called :");
    NSLog(@"Event: %@", event);
    NSLog(@"Point: %@", NSStringFromCGPoint(point));
    NSLog(@"Event Type: %d", event.type);
    NSLog(@"Event SubType: %d", event.subtype);
    NSLog(@"---");

    return [super hitTest:point withEvent:event];
}
4

1 回答 1

1

好的,我专门为你做了示例项目。这是我所做的:

  • 子类化 UIView 类并创建 CustomView。
  • 在身份检查器部分的情节提要中,将视图的类设置为 CustomView

在此处输入图像描述

在屏幕截图上,您可能会注意到视图层次结构,它重复了您的概念。

这里hitTest:withEvent在 CustomView.m 中被覆盖:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
        for (UIView *subview in self.subviews.reverseObjectEnumerator) {
            CGPoint subPoint = [subview convertPoint:point fromView:self];
            UIView *result = [subview hitTest:subPoint withEvent:event];
            if (result != nil && [result isKindOfClass:[UIButton class]]) {
                return result;
            }
        }
    }

    return [super hitTest:point withEvent:event];
}

该方法通过调用pointInside:withEvent:每个子视图的方法来遍历视图层次结构,以确定哪个子视图应该接收触摸事件。如果pointInside:withEvent:返回YES,则类似地遍历子视图的层次结构,直到找到包含指定点的最前面的视图。如果视图不包含该点,则忽略其视图层次结构的分支。您很少需要自己调用此方法,但您可能会覆盖它以隐藏子视图中的触摸事件. 此方法忽略隐藏、禁用用户交互或 alpha 级别小于 0.01 的视图对象。此方法在确定命中时不考虑视图的内容。因此,即使指定点位于视图内容的透明部分中,仍可以返回视图。

关于甜点示例项目

于 2014-04-07T14:31:53.013 回答