I have a regular PKCanvasView implemented inside a UIViewRepresentable in SwiftUI.
struct Canvas2View: UIViewRepresentable {
@Binding var canvasView: MyPKCanvasView
@Binding var toolPickerIsActive: Bool
private let toolPicker = PKToolPicker()
let onChange: () -> Void
func makeUIView(context: Context) -> PKCanvasView {
canvasView.backgroundColor = .clear
canvasView.isOpaque = false
canvasView.delegate = context.coordinator
canvasView.minimumZoomScale = 1
canvasView.maximumZoomScale = 4.0
canvasView.showsVerticalScrollIndicator = false
canvasView.showsHorizontalScrollIndicator = false
showToolPicker()
return canvasView
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
toolPicker.setVisible(toolPickerIsActive, forFirstResponder: uiView)
}
func showToolPicker() {
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
func makeCoordinator() -> Coordinator {
Coordinator(canvasView: $canvasView, toolPicker: toolPicker, onChange: onChange)
}
}
Then I added an imageview when the layout happens:
class MyPKCanvasView: PKCanvasView {
var imageView = UIImageView(image: UIImage(named: "cat"))
override func layoutSubviews() {
super.layoutSubviews()
if imageView.superview == nil {
imageView.frame = bounds
imageView.contentMode = .scaleAspectFit
insertSubview(imageView, at: 0)
}
}
}
Everything seems to work great. I have an image behind the sketch.
The last step now is I want to blend the two graphics with the multiple blend mode.
I have attemped:
override func layoutSubviews() {
super.layoutSubviews()
if imageView.superview == nil {
imageView.frame = bounds
imageView.contentMode = .scaleAspectFit
self.layer.compositingFilter = "darkenBlendMode" ///tried this
insertSubview(imageView, at: 0)
}
}
was an example from another question How to get multiply blend mode on a plain UIView (not UIImage)
but it has no effect. How to do it?