1

I have a container view that contains the view of a UIPageViewController. This is inside a UIViewController and takes up the whole screen. On top of the container view I have a UIView, covering half the screen, which contains a button and some text. I want to forward the touches from this UIView to the UIPageViewController. This is so that the UIPageViewController can still be swiped left/right even if the user is swiping over the UIView. I also want the button to be able to be pressed, therefore can't just set isUserInteractionEnabled to false on the UIView.

How can I do this?

Nishant Bhindi
  • 2,168
  • 6
  • 21
Tometoyou
  • 5,817
  • 6
  • 46
  • 83
  • 1
    Possible duplicate of [Passing through touches to UIViews underneath](https://stackoverflow.com/questions/9026097/passing-through-touches-to-uiviews-underneath) – Jakub Truhlář May 23 '17 at 10:20

2 Answers2

1

hitTest is the method which determines who should consume the touches/gestures.

So your "UIView, covering half the screen" can subclass from say NoTouchHandlerView like. And then this view will not consume touches. It would pass then to views under it.

class NoTouchHandlerView: UIView
{
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
    {
        if let hitTestView = super.hitTest(point, with: event), hitTestView !== self {
            return hitTestView
        }else {
            return nil
        }
    }
}
BangOperator
  • 4,057
  • 2
  • 21
  • 37
0

Objective C version of the accepted answer for lazy guys like me :)

@implementation NoTouchHandlerView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView* hitTestView = [super hitTest:point withEvent:event];

    if (hitTestView != nil && hitTestView != self) {
        return hitTestView;
    }

    return nil;
}

@end
Matheus Lima
  • 101
  • 2
  • 6