Search code examples
swiftuiviewtouchesuievent

UIView pass through touches only if more than one finger?


My UIView already handles single-finger touches with gesture recognisers for tap and pan just fine.

However, I would like touches with two fingers to be passed through to the parent view, behind this view. (The parent is a WKWebView with a javascript generated map that I'd like the user to be able to pinch-zoom, even while this other view is in front. This works OK when the other view is not in front, but of course when the front view is there, it doesn't pass through the touches.)

I have tried to detect this using either of the following in the front view, but in both cases, allTouches is an empty set (zero touches):


    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if let event = event {
            print("\(event)")
            if let touches = event.allTouches {
                print("\(event.allTouches)")
                if touches.count > 1 {
                    return false
                }
            }
        }
        
        return super.point(inside: point, with: event)
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if let event = event {
            print("\(event)")
            if let touches = event.allTouches {
                print("\(event.allTouches)")
                if touches.count > 1 {
                    return nil
                }
            }
        }
        
        return super.hitTest(point, with: event)
    }

How can I continue to use my existing gesture recognisers, but pass through multi-finger touches to the superview?


Solution

  • More of a work-around than an actual answer, but here's what solved it for me...

    Instead of the in-front child view covering all of the parent view with a transparent layer, I reduced it to only include the small area needed to actually present information and form fields to the user.

    Then I changed the single-touch gestures to be added to the parent view, instead of to the child view.

    So now NONE of the touches are captured by the child view (except for its form fields and buttons).

    This is perhaps the better way to handle my particular situation anyhow. So I was probably asking the wrong question (X/Y issue!).