Question

I have BooksEAGLView and it has UIButton for receiving touch event. Then my purpose of Augmented Reality overlay I am adding overlay view to BooksEAGLView then my button is not receiving touch event.

How can i get touch event of both view.

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]]);
 }

Touch Event:

- (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];
}
Was it helpful?

Solution

Ok, I did the sample project specially for you. Here what I did:

  • Subclassed UIView class and created CustomView.
  • In the storyboard in the Identity inspector section set the view's Class as CustomView

enter image description here

On screenshot you may notice the view hierarchy, it repeats your concept.

Here is overridden hitTest:withEvent in 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];
}

This method traverses the view hierarchy by calling the pointInside:withEvent: method of each subview to determine which subview should receive a touch event. If pointInside:withEvent: returns YES, then the subview’s hierarchy is similarly traversed until the frontmost view containing the specified point is found. If a view does not contain the point, its branch of the view hierarchy is ignored. You rarely need to call this method yourself, but you might override it to hide touch events from subviews. This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.

On the dessert: Sample Project

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top