This is the simplest reproduction code; Lasso works in 16.4, but not in 17.
import SwiftUI
import PencilKit
struct ContentView: View {
@State var canvas = CanvasTest()
var body: some View {
VStack {
canvas
Text("Pen")
.onTapGesture {
canvas.canvasView.tool = PKInkingTool(ink: PKInk(.pen), width: 4)
}
Text("Lasso")
.onTapGesture {
canvas.canvasView.tool = PKLassoTool()
}
}
.padding()
}
}
struct CanvasTest: UIViewRepresentable {
let canvasView = PKCanvasView()
func makeUIView(context: Context) -> some PKCanvasView {
canvasView.drawingPolicy = .anyInput
canvasView.tool = PKInkingTool(ink: PKInk(.pen), width: 20)
canvasView.delegate = context.coordinator
return canvasView
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
}
extension CanvasTest {
final class Coordinator: NSObject, PKCanvasViewDelegate {
var parent: CanvasTest
init(_ uiView: CanvasTest) {
self.parent = uiView
}
}
func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {
canvasView.tool = PKLassoTool()
}
}
Post
Replies
Boosts
Views
Activity
Sorry, this one is even simpler.
import SwiftUI
import PencilKit
struct ContentView: View {
@State var canvas = CanvasTest()
var body: some View {
VStack {
canvas
Text("Pen")
.onTapGesture {
canvas.canvasView.tool = PKInkingTool(ink: PKInk(.pen), width: 4)
}
Text("Lasso")
.onTapGesture {
canvas.canvasView.tool = PKLassoTool()
}
}
.padding()
}
}
struct CanvasTest: UIViewRepresentable {
let canvasView = PKCanvasView()
func makeUIView(context: Context) -> some PKCanvasView {
canvasView.drawingPolicy = .anyInput
canvasView.tool = PKInkingTool(ink: PKInk(.pen), width: 20)
return canvasView
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
}