Search code examples
iosswiftuipageviewcontroller

Forward touches to UIPageViewController


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?


Solution

  • 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
            }
        }
    }