Since updating to iOS17, lasso no longer works in Pencilkit with SwiftUI support using UIViewRepresentable. It works in PKToolPicker, but the programmatically created PKLassoTool does not work. How can I get lasso to work?
Accepted Reply
iOS 17.1 appears to me to have resolved this issue.
-
We also checked it today and plan to update it in this version. Thank you all for your hard work
Replies
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()
}
}
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) {
}
}
Our service is also experiencing the same problem. Is there still no solution?
-
Thank you for your comment. Unfortunately, this issue has not yet been resolved and we are inquiring with TSI. When we get an answer we will share it with you.
-
Following the response from Technical Support, I filed a bug report with Feedback Assistant. According to Technical Support, OS or framework fix is probably needed. If we have further information, we will share it.
-
That's very unfortunate. Thank you for the information!
My solution is a bit hacky. I added the built-in tool PKToolPicker and displayed the tools. When selecting Text("Lasso"), I set self.toolPicker.setVisible(true, forFirstResponder: context.coordinator) so that PKLassoTool can be used. When selecting other tools, I set self.toolPicker.setVisible(false, forFirstResponder: context.coordinator).
It's quite cumbersome, and I hope there is a better way.
let toolPicker = PKToolPicker()
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, PKToolPickerObserver {
....
}
Just to add that this is also affecting my UIKit app too. Any update yet?
-
I have not received any response from Apple's engineering team. I will let them know that you are having the same problem with UIKit.
-
Thank you!
iOS 17.1 appears to me to have resolved this issue.
-
We also checked it today and plan to update it in this version. Thank you all for your hard work