0

I want to use pencil kit in my swiftui app. I encapsulate PKCanvasView in UIViewRepresentable. I can draw on canvas with only one stroke. However, the image will disappear immediately when I release the finger.

Am I missing anything?

import SwiftUI
import PencilKit

struct ContentView: View {
    var body: some View {
        CanvasView()
    }
}

struct CanvasView: UIViewRepresentable {
    func makeUIView(context: Context) -> PKCanvasView {
        let canvas = PKCanvasView()
        canvas.tool = PKInkingTool(.pen, color: .green, width: 10)
        canvas.drawingPolicy = .anyInput
        return canvas
    }
    
    func updateUIView(_ uiView: PKCanvasView, context: Context) {
        
    }
}

EDIT:

Xcode14.1, simulator iOS16.1

test1229
  • 125
  • 9

1 Answers1

0

I had the same problem using an iPad Pro 11" or mini simulator, while the iPhone simulator worked as expected. However, this seems to be a simulator glitch, since it works well on an actual iPad mini device.

Erwin
  • 555
  • 6
  • 8