如何在包装器 Swift 视图中使用自定义 UIView 的扩展功能?

How to use extended function of the custom UIView in a wrapper Swift View?

(在 Xcode 11.3、Swift 5.1.3 中测试)

我想扩展 UIView,用 UIViewRepresentable 包装它,并将其用作 SwiftView。但是,我很难从包装器 Swift 视图访问自定义 UI 视图的扩展功能。

class UICameraCaptureImageView: UIImageView, AVCaptureVideoDataOutputSampleBufferDelegate {
  @State var capturedImage: UIImage? = UIImage(named: "default_placeholder")

  func startCameraCapture()  {  
    // start camera capture when it is ready
  }

  // AVCaptureVideoDataOutputSampleBufferDelegate delegate method follows
  // ...

}
struct CameraCaptureImageView: UIViewRepresentable {

  // cannot set containedUIView in makeUIView/updateUIView, and they are not mutating method
  private var containedUIView: UICameraCaptureImageView?

  func makeUIView(context: UIViewRepresentableContext<CameraCaptureImageView>) -> 
      UICapturedImageView {
    UICapturedImageView()     
  }

  func updateUIView(_ uiView: UICapturedImageView, 
      context: UIViewRepresentableContext< CameraCaptureImageView>) {
    uiView.image = capturedImage
  }

  func startCameraCapture()  {  
    // redirect to UICameraCaptureImageView.startCameraCapture(), 
    // but cannot set self.containedUIView
    guard self.containedUIView != nil else {
      print("The containedUICaptureView doesn't exist")
      return
    }
    self.containedUIView?.startCameraCapture()
  }
}

起初,虽然是一种有状态的策略,但我尝试在CameraCaptureImageView中声明一个成员变量,并在创建时设置UICameraCaptureImageView实例。但如您所见,makeUIView() 未声明为变异方法,因此我无法改变 CameraCaptureImageView 的任何成员。

如何从 UIViewRepresentable 包装器访问我的 UIView 子类中的扩展自定义函数 startCameraCapture()?或者,是否有任何无状态的、体面的解决方案来使用 SwiftUI 中的扩展旧 UIView?

您应该创建一个 Coordinator 来为您管理这种穿梭。它是一个 class,因此并不严格依赖于非变异语义。

struct CameraCaptureImageView: UIViewRepresentable {

  func makeUIView(context: UIViewRepresentableContext<CameraCaptureImageView>) -> 
      UICapturedImageView {
    return UICapturedImageView()     
  }

  func makeCoordinator() -> Coordinator {
    return .init(self)
  }
}

extension CameraCaptureImageView {

  // add any delegate/protocol conformances here, it's just an object!
  private class Coordinator  {
    let cameraCaptureImageView: CameraCaptureImageView

    init(_ cameraCaptureImageView: CameraCaptureImageView) {
       // CameraCaptureImageView is a `struct`, so it's a copy!
       self.cameraCaptureImageView = cameraCaptureImageView
    }

    // now here is all your UIView specific logic
  }
}

需要发出信号吗?在你的 View 上添加一个闭包,你的 Coordinator 可以在某些事件上调用。

struct CameraCaptureImageView: ... {

    let onSomeEvent: (Event) -> Void
}

class Coordinator {

    let cameraCaptureImageView: ...

    func view(_ view: UIViewOfSomeKind, didReceive event: Event) {
      cameraCaptureImageView.onEvent(event)
    }
}