Surya Subenthiran
Surya Subenthiran

Reputation: 2217

How to let two overlapping views to receive the user touch events ios

I have my UIWebView respond to touch event from user, and also have UIView that overlaps the UIWebView partially. I added a Pan gesture recognizer to the overlapped view. When user touching the overlapped view it blocks the webview touch events. Is there any possible way the Webview receives touch event when Overlapped view tapped? Thanks

Upvotes: 3

Views: 1552

Answers (1)

gabbler
gabbler

Reputation: 13766

If you only want to click through the overlapped area, overlay view can subclass UIView and implement hitTest:withEvent:, below will make the overlapped area transparent.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView == self && CGRectContainsPoint(overlappedArea, point)) {
    return nil;
   }
    return hitView;
}

But this will cancel all touches in overlapped area of overlay view , thus makes the pan gesture fail. Another way is to add pan gesture and tap gesture to their owned superview, in area of overlay view, enable pan gesture, in area of underlying view, enable tap gesture. Like this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [touch locationInView:self.view];

    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        if (CGRectContainsPoint(self.overlayView.frame, touchPoint)) {
            return YES;
        } else {
            return NO;
        }
     } else if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        if (CGRectContainsPoint(self.underlyingView.frame, touchPoint)) {
            return YES;
        } else {
            return NO;
        }
    }
    return YES;
}

Upvotes: 3

Related Questions