0

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];
}
user3496826
  • 101
  • 2
  • 14

1 Answers1

1

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

Neil Galiaskarov
  • 5,015
  • 2
  • 27
  • 46
  • My problem is here [eaglView addSubview:bookOverlayController.view]; I'm adding overlay content to eaglView. So the bookOverlayController.view hiding my eaglView UIButton. How to solve this? – user3496826 Apr 07 '14 at 14:35
  • @user3496826, as you see, I've overridden the method of class, which holds both views (overlay and button) – Neil Galiaskarov Apr 07 '14 at 14:40