Interacting with a SwiftUI view below a UIKit PKCanvasView

Viewed 357

I have a view Structure as follows:

ZStack {
    UIKitView //PKCanvasView
        .zIndex(1)
    SwiftUIView //SwiftUI view which has a gesture recogniser 
        .zIndex(0)
}

I wanted to know what the best way was to allow simultaneous interaction to the SwiftUI view below the UIKit view.

This would work best if the SwiftUI view had a higher priority gesture over the UIKit view on top.

My UIKit representable view is as follows:

struct CanvasView: UIViewRepresentable {
    
    @Binding var canvasView: PKCanvasView
    @Binding var isFirstResponder: Bool
    
    @State var toolPicker = PKToolPicker()
    
    func makeUIView(context: Context) -> PKCanvasView {
        
        canvasView.drawingPolicy = .pencilOnly
        canvasView.backgroundColor = .clear
        canvasView.isOpaque = false
        canvasView.alwaysBounceVertical = true
        canvasView.alwaysBounceHorizontal = true
        
        
        toolPicker.setVisible(true, forFirstResponder: canvasView)
        toolPicker.addObserver(canvasView)
        //canvasView.becomeFirstResponder()
        
        return canvasView
    }
    
    func updateUIView(_ uiView: PKCanvasView, context: Context) {
        if isFirstResponder != uiView.isFirstResponder {
           if isFirstResponder {
               uiView.becomeFirstResponder() 
           } else {
               uiView.resignFirstResponder()
           }
       }
    }
    
}

Ideally I would like to handle the layering of views in SwiftUI as in reality a have multiple SwiftUI views below the canvas view which when held & dragged (that is the compound gesture I am applying to them) should appear above it. (This is achieved using dynamic zIndexs). I'm not comfortable enough with UIKit to know how to do this there with subviews and a UIViewRepresentable structure.

Any help would be greatly appreciated!

0 Answers
Related