2

I'm developing a small PencilKit app and have placed a UIViewRepresentable on top of it in a ZStack to handle gesture input (to rotate, scale and pan the canvas around the screen). But I'm struggling to get that top view to ignore PencilKit input for drawing on the canvas below. When the Gesture View is active, nothing can get through to the canvas. Turning hit testing or user interaction off disables all input to the Gesture view. Is there a way to pass certain inputs through to the view below?

CodeyTown
  • 87
  • 4

1 Answers1

1

Without showing the actual implementation might be difficult to help you in this case but what you could try to do is overriding the hitTest function of your UIView. An example could be this:

// by turning on this flag 
// the view would pass all events to the 
// underlying view
var passEventsToUnderlyingViews = false
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    guard passEventsToUnderlyingViews else {
        return super.hitTest(point, with: event)
    }
    let view = super.hitTest(point, with: event)
    return view == self ? nil : view
}
MarcoCarnevali
  • 671
  • 8
  • 23
  • Sorry for the late reply. This fixed it. Thanks very much. – CodeyTown May 20 '22 at 07:09
  • 1
    Hi @CodeyTown, would you share how you implemented MarcoCarnevali's solution? I'm facing a similar issue and can't make this work. Thx! – Juan Jan 11 '23 at 16:21
  • 1
    Hi. I'm afraid I don't have access to that code base anymore so can't check. But from memory this method was replaced in the final implementation anyway. Instead, each type of possible gesture was handled with a UIGestureRecognizer which would call the necessary method. Drawing with the Apple Pencil I think was handled with a UIPanGestureRecognizer. Sorry I can't be more help. – CodeyTown Jan 18 '23 at 13:07