Post not yet marked as solved
In iOS whenever you use the PKLassoTool on a set of strokes if you click on the selected region a menu will pop up as you can see here
Is there a way to add more buttons to this menu?
I tried looking through the Swift docs but I haven't seen this mentioned anywhere.
Post not yet marked as solved
We've been using PencilKit in our app for a couple of years now, but have run into a significant slowdown recently. It coincided with a shift to iPadOS 17, but possibly that's a coincidence.
It seems to occur when we add PKStroke elements to a PKDrawing (programatically). Previously this has refreshed on-screen instantly, but now it's so slow we can sometime see the new stroke progressively drawing on-screen. I profiled the running app and the lag seems to entirely occur within the following call sequence:
2563 -[PKDrawingConcrete addNewStroke:]
2494 -[PKDrawing setNeedsRecognitionUpdate]
2434 -[NSUserDefaults(NSUserDefaults) boolForKey:]
2419 _CFPreferencesGetAppBooleanValueWithContainer
2417 _CFPreferencesCopyAppValueWithContainerAndConfiguration
2399 -[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]
2395 -[_CFXPreferences withSearchListForIdentifier:container:cloudConfigurationURL:perform:]
2394 normalizeQuintuplet
2367 __108-[_CFXPreferences(SearchListAdditions) withSearchListForIdentifier:container:cloudConfigurationURL:perform:]_block_invoke
2338 __76-[_CFXPreferences copyAppValueForKey:identifier:container:configurationURL:]_block_invoke
2336 -[CFPrefsSource copyValueForKey:]
2332 -[CFPrefsSearchListSource alreadylocked_copyValueForKey:]
1924 -[CFPrefsSearchListSource alreadylocked_copyValueForKey:].cold.1
1921 _os_log_debug_impl
1918 _os_log
1914 _os_log_impl_flatten_and_send
1829 _os_log_impl_stream
1811 _os_activity_stream_reflect
1205 dispatch_block_perform
1199 _dispatch_block_invoke_direct
1195 _dispatch_client_callout
1194 ___os_activity_stream_reflect_block_invoke
1188 _xpc_pipe_simpleroutine
882 _xpc_send_serializer
867 _xpc_pipe_mach_msg
859 mach_msg
859 mach_msg_overwrite
855 mach_msg2_internal
853 0x1cf7701d8
Up to 'addNewStroke' this makes sense. But where is it going with 'PKDrawing setNeedsRecognitionUpdate'? This ultimately seems to be hitting some kind of lock, which I assume is the cause of the lag.
Any suggestions for why this is happening or a means of avoiding it would be much appreciated.
Post not yet marked as solved
I'm using PkCanvasView one of my app for drawing purpose. When I use lasso tool for selection and tap on selected area, menu controller appear with options "Cut, Copy, Delete, Duplicate, Insert Space Above". As per my app requirement, I want to remove "Insert Space Above" from menu controller.
Is it doable?
Post not yet marked as solved
You used to be able to select a drawing, long press to show the edit menu (copy, duplicate, delete, ...), copy the item and paste it after another long press on the canvas. However, since iOS / iPadOS 16.1 long pressing the canvas does not show any menu.
The action still works if you connect an external mouse to your iPad, activate the secondary click functionality and use the right mouse button to click on the canvas. This shows the menu and you can paste the copied drawing.
It seems to be broken in all PencilKit-based apps, including Apple's sample apps. Is there any workaround? This is a major problem for my app and used to work fine since the introduction of PencilKit with iOS 13.
Post not yet marked as solved
I’m building a drawing app and I want to add more things I can do with the strokes selected by the PKLassoTool. Is there a way to edit the menu of buttons that appears when you make a selection with the lasso tool? And then is there away to pass the selected strokes into a function?
I’ve looked at the Apple documentation but there doesn’t seem to be a lot of support to doing anything with the lasso tool.
Post not yet marked as solved
I am using Xcode 15 beta 5 with iOS 17 beta 4 SDK. By building with the iOS 17 SDK my app got all the new inks which work great. Saving/encoding the PKDrawing to Data also works fine. However if I want to load the same PKDrawing again, on the same simulator or device (i.e., same iOS version) it fails with "Apple Drawing Format is from a future version that is too new.".
From my understanding, reading https://developer.apple.com/documentation/pencilkit/supporting_backward_compatibility_for_ink_types?changes=_2 this is the expected behaviour when trying to load such a PKDrawing on an older iOS version which doesn't support the new ink types.
Here is a short example, which prints the error:
var drawing = PKDrawing()
let strokePoints = [
PKStrokePoint(location: CGPoint(x: 0, y: 0), timeOffset: 0, size: CGSize(width: 3.0, height: 3.0), opacity: 2, force: 1, azimuth: 1, altitude: 1)
]
let strokePath = PKStrokePath(controlPoints: strokePoints, creationDate: Date())
drawing.strokes.append(PKStroke(ink: .init(.watercolor), path: strokePath))
do {
let data = drawing.dataRepresentation()
let drawing2 = try PKDrawing(data: data)
print("success")
} catch {
print(error)
}
}
Saving & loading a PKDrawing which does not use any of the new ink types works fine.
Hello, I have a issue that's PKCanvasView's delegate method (canvasViewDrawingDidChange) is automatically called when the view present with .popover setting. In my case, I want to show some popup view on PKCanvasView and that's happening.
viewController.modalPresentationStyle = .popover
When I comment out this line the delegate method not getting called and everything is fine.
The biggest problem was all previous ink on PKCanvas deleted somehow if this delegate being called automatically. Is this PencilKit bugs?
Note: this trigger only with Apple Pencil, finger touch is fine.
Post not yet marked as solved
Hi,
I'm trying to use PencilKit over PDFKit as described in https://developer.apple.com/videos/play/wwdc2022/10089/. The thing is I open my custom UIDocument and initialize all its content to feed PDFView. Everything seems to work, I Input sketches in the canvas, PDFPageOverlayViewProvider's overlayView(for:) generates canvas correctly (it seems) but when editing finishes : willEndDisplayingOverlayView never gets called, and when I save the UIDocument (I use document.close(completionHandler:)) contents(forType:) never sees my custom PDFPages and I get no content for sketches.
Does anyone of you have an idea of the lifecycle we should follow to get the methods called ?
Sincerely yours
Post not yet marked as solved
My problem
I have a SwiftUI app that uses PKCanvasView to allow drawing. The PKCanvasView is transparent and there are SwiftUI views (e.g. an Image) behind which can be dragged and opened.
I want the user to be able to draw on the PKCanvasView with their apple pencil and interact with the views below with their finger...
I can't figure out how to enable simultaneous interaction with the PKCanvasView (i.e. if using apple pencil draw) and the swiftUI views behind (i.e. if using finger, drag and move views).
The context
Here is my PKCanvasView wrapped in a UIViewRepresentable struct:
struct CanvasView: UIViewRepresentable {
@Binding var canvasView: PKCanvasView
@State var toolPicker = PKToolPicker()
func makeUIView(context: Context) - PKCanvasView {
canvasView.drawingPolicy = .pencilOnly
canvasView.backgroundColor = .clear
canvasView.isOpaque = false
canvasView.alwaysBounceVertical = true
canvasView.alwaysBounceHorizontal = true
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
return canvasView
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
}
}
The structure of my content view is as follows:
swift
ZStack(alignment: .topLeading){
CanvasView(canvasView: $canvasView)
.zIndex(0)
ForEach(canvas.subCanvases.indices, id: \.self){ index in
CanvasCardView(
canvas: $canvas.subCanvases[index],
animationNamespace: animationNamespace,
onReorder: {
reorderCard(at: canvas.subCanvases[index].zOrder)
})
}
}
The CanvasCardView are the background SwiftUI views which can be dragged and opened. The view attaches a negative zIndex to them which orders them behind the CanvasView (PKCanvasView).
My attempts
I've tried two approaches so far which don't work:
Subclassing PKCanvasView to override point(inside:with:). Passing false when I want to touches to be passed through. Problem with this is that the swiftUI views never recognise the touches through the UIView. I assume as there are no UITouchs behind the scenes.
Adding a @State property to my contentView that can update an allowsHitTesting() modifier dynamically when required. The issue with this is that without allowing the PKCanvasView to be "hit" I can't determine whether it is a pencil touch or not. So i can't properly interact with PKCanvasView simultaneously.
Any other ideas if this is possible? That is, passing touches through a UIViewRepresentable dynamically?
I really want to avoid shoving the SwiftUI views into a UIView (with UIHostingController) and then turning them back into SwiftUI.
Many thanks!
I've been trying to disable the "Smart Selection" feature introduced in https://developer.apple.com/wwdc20/10107 from a PKCanvasView. This feature could be very useful for some apps but if you want to start from a clean state canvas it might get in your way as you add gestures and interactions.
Is there any way to opt out from it?
The #WWDC20-10107 video demonstrates the "Smart Selection" feature at around 1:27.
Post not yet marked as solved
Hi everyone!
I was wondering if anyone has tips or resources to help me make a view (ideally in swiftUI) that works the same as a board in the Freeform app from Apple?
I search online but I couldn't find anything that would help me
I would like to be able to add views that I can drag around and interact with plus I would need to have pencilKit support and be able to move around the parent view (the Freeform board like view)
Thanks
Post not yet marked as solved
We're noticing some odd behaviour using PencilKit in our app. Most of the time, the response it quite fluid. But at intervals (and depending on what kind of strokes we create), PencilKit freezes the whole app for up to several seconds. It will come to life again and continue as normal, but eventually repeat that behaviour. When it happens, it's usually accompanied by one or two lines of console output, e.g.:
2023-05-12 15:52:29.101996+0100 Spaces[51229:3635610] [] Did not have live interaction lock at end of stroke
2023-05-12 15:52:29.556467+0100 Spaces[51229:3630745] [] Drawing did change that is not in text.
I can't find any reference to these messages. They may have no bearing on the observed problem, but it might be a clue. Has anyone seen this behaviour or have any information about their meaning?
Post not yet marked as solved
I have PKCanvasView in my iPad app. When the user rotates the iPad - I need to scale the Canvas and all the strokes accordingly.
So far I've found two ways to scale a PKCanvasView, first with a .zoomScale:
GeometryReader { geo in
// Canvas View goes here...
}
.onChange(of: geo.size) { newSize in
let canvasScale = newSize.width / myDefaultCanvasWidth
canvasView.minimumZoomScale = canvasScale
canvasView.maximumZoomScale = canvasScale
canvasView.zoomScale = canvasScale
}
Second with CGAffineTransform:
//For short:
let transform = CGAffineTransform(scaleX: scaleWidth, y: scaleHeight)
canvasView.drawing = canvasView.drawing.transformed(using: transform)
Please help me understand the difference between those two methods and which one is correct and preferable?
Thank you.