PencilKit

RSS for tag

Capture touch input as an opaque drawing and turn it into high-quality images that can be displayed on iOS and macOS using PencilKit.

Posts under PencilKit tag

134 Posts

Post

Replies

Boosts

Views

Activity

SwiftUI Third Party Keyboard
Hello. I am developing an app that helps people with language issues communicate and for that I thought I could do a custom keyboard for them. I was wondering if there is a way of doing a 3rd party keyboard using SwiftUI (little to no UIKit) and if so, where can I find documentation/tutorials. I have looked with no success. Additionally, can we use a PKCanvasView inside of keyboards (ie, is there any restriction/policy)? I intend to use it so that users can draw the words they're looking for (I already have the model and know how to come up with the suggestions and everything) Thanks a lot!
0
0
909
Nov ’23
PencilKit: zoom PKCanvasView, drawing blurred (swift, iOS 15)
It seems PKCanvasView overrides the property UIScrollViewDelegate which inherits from the UIScrollView to PKCanvasViewDelegate. And does not provide access to UIScrollViewDelegate. In order to implement zooming, so I added a PKCanvasView into my own UIScrollView. And implemented delegate method viewForZooming in which return PKCanvasView. But all drawing in PKCanvasView was blurred when zooming or scale. How to re-render drawing after zoom to make it has reasonable stroke width and clear ? Some related code: let canvasView = PKCanvasView() let scrollView = UIScrollView() override func viewDidLoad() {     super.viewDidLoad()     self.view.addSubview(scrollView)     scrollView.addSubview(canvasView)     scrollView.delegate = self     scrollView.minimumZoomScale = 0.5     scrollView.maximumZoomScale = 2.5 } func viewForZooming(in scrollView: UIScrollView) -> UIView? {     return canvasView } Some solutions I had tried: 1: Reset PKCanvasView contentScaleFactor func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {     if let canvas = view {       let contentScale = scale * UIScreen.main.scale       canvas.contentScaleFactor = contentScale     }   } Not worked! 2: Re-render PKStroke: func reRender(_ scale: CGFloat) {     let newStrokeWidth = strokeWidth * scale     var newDrawingStrokes: [PKStroke] = []     for stroke in canvasView.drawing.strokes {       canvasView.tool = PKInkingTool(.pen, color: .red, width: newStrokeWidth)       var newPoints = [PKStrokePoint]()       stroke.path.forEach { (point) in         let newPoint = PKStrokePoint(location: point.location,                        timeOffset: point.timeOffset,                        size: CGSize(width: newStrokeWidth, height: newStrokeWidth),                        opacity: CGFloat(1), force: point.force,                        azimuth: point.azimuth, altitude: point.altitude)         newPoints.append(newPoint)       }       let newPath = PKStrokePath(controlPoints: newPoints, creationDate: Date())       let newStroke = PKStroke(ink: PKInk(.pen, color: UIColor.red), path: newPath)       newDrawingStrokes.append(newStroke)     }     let newDrawing = PKDrawing(strokes: newDrawingStrokes)     canvasView.drawing = newDrawing   } Not worked! Still blurred, just changed strokeWidth by multiply scale. 3: I try to reset PKDrawing or PKStroke transform by using scrollView scale. Then PKDrawing position disordered and it was still blurred. Please help me.
5
4
5.8k
Oct ’23
Swift PKLassoTool menu
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.
0
1
711
Oct ’23
Severe lag adding PKStroke to PKDrawing
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.
0
1
799
Oct ’23
PKCanvasView no longer shows edit (paste) menu on long press (iOS 16.1)
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.
7
1
3.5k
Oct ’23
Swift PKLassoTool custom menu
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.
2
3
985
Sep ’23
Can't load/decode PKDrawing using any of the new inks (version 2, iOS 17)
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.
1
3
1.4k
Aug ’23
PencilKit's delegate method being called when used .popover style
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.
1
0
962
Aug ’23
wwdc2022-10089 issue
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
2
0
1.2k
Jul ’23
Simultaneously interacting with SwiftUI View below a UIViewRepresentable
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!
3
2
2.9k
Jul ’23
Making a Freeform board style view
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
0
1
1.2k
Jun ’23
Laggy PencilKit stroke response
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?
1
1
1.3k
May ’23
What is a proper way to scale PKCanvasView (UIScrollView)?
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.
0
0
1.2k
Apr ’23
How to create .drawing file for PencilKit?
I would like to ask if I have the following code : import PencilKit import SwiftUI struct ContentView : View{ var canvas = PKCanvasView() var picker = PKToolPicker() var body: some View{ MyPencilView(canvas : canvas, picker: picker) } } struct MyPencilView: UIViewRepresentable{ var canvas: PKCanvasView let picker: PKToolPicker func makeUIView(context: Context) -> some UIView { picker.setVisible(true, forFirstResponder: canvas) picker.addObserver(canvas) canvas.becomeFirstResponder() return canvas } func updateUIView(_ uiView: UIViewType, context: Context) { } } Where should I add dataRapresentation() to save the . drawing file in the asset folder? Thanks in advance
1
0
1.3k
Apr ’23
touch.force unit - collecting data from Apple Pencil
Hello, I am currently working in an Alzheimer's related researching where we are utilizing the force (pressure) data from the Apple Pencil.  While we are able to get a range of the force values (0 - 4.2), we do not know the actual unit of this pressure. I have gone through the documentation but I did not find any mention about the unit of the touch.force parameter.  What is the unit of this touch.force parameter?  I also wanted to ask about being able to capture other information from the apple pencil like accelerometer data, gyroscope data etc. The more data we are able to capture from the pencil the better, so that the data can be used for future studies about Alzheimer's Disease. Currently I am using the pencil to capture the following data: - Force Location (coordinates) Altitude angle Azimuth angle Azimuth Unit Vector Perpendicular Force (calculated as per documentation) Major Radius Tap Count If there is anything more that I can capture via the apple pencil, it would be great for future studies.  Additionally, a little about the application, we are having users draw a clock on a canvas and collecting that data. I am using a PKCanvasView for the canvas, and if there is any data that I can collect via the canvas, I would love to know that as well. As of now, I am using the PKCanvasViewDelegate to get data, but if there is anything more that I can collect, that would be great.  Thank you for your time
0
0
941
Mar ’23
How to create multiple layers in PKCanvasView?
How can I use PKCanvasView in PencilKit to create multiple layers? These views are superimposed on each other. I want the drawing of each layer is kept independent from each other. It's also important that when the user uses the drag or zoom gesture on it, all layers MUST be scaled or dragged at the same time. Please provide a more concise, efficient and appropriate code implementation. Thanks.
0
1
1.3k
Mar ’23
How to increase the width of PencilKit .pen stroke?
The default PKInkingTool with inkType .pen seems to be limited in its stroke width. With a maximum width of around 25.66. Why is that? How can I increase it? Example code: canvasView.tool = PKInkingTool(.pen, color: .black, width: 10) print(canvasView.tool) //Prints: PKInkingTool(tool: <PKInkingTool: 0x2837ba140 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=10.000000>) canvasView.tool = PKInkingTool(.pen, color: .black, width: 20) //Prints: PKInkingTool(tool: <PKInkingTool: 0x282730140 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=20.000000>) //Problem starts here: canvasView.tool = PKInkingTool(.pen, color: .black, width: 30) //Prints: PKInkingTool(tool: <PKInkingTool: 0x28077a0c0 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=25.659260>) canvasView.tool = PKInkingTool(.pen, color: .black, width: 100) //Prints: PKInkingTool(tool: <PKInkingTool: 0x2824e08e0 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=25.659260>) How can I increase the stroke width? 🤔
2
0
1.4k
Feb ’23
SwiftUI Third Party Keyboard
Hello. I am developing an app that helps people with language issues communicate and for that I thought I could do a custom keyboard for them. I was wondering if there is a way of doing a 3rd party keyboard using SwiftUI (little to no UIKit) and if so, where can I find documentation/tutorials. I have looked with no success. Additionally, can we use a PKCanvasView inside of keyboards (ie, is there any restriction/policy)? I intend to use it so that users can draw the words they're looking for (I already have the model and know how to come up with the suggestions and everything) Thanks a lot!
Replies
0
Boosts
0
Views
909
Activity
Nov ’23
Lasso no longer works with iOS17
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?
Replies
6
Boosts
4
Views
2.3k
Activity
Oct ’23
Editing in duplicate page is affecting original Page iOS17
I am showing PDF file in QLPreview Controller, in iOS17, after copy and paste the page of pdf and edit the original page using Pencil kit, will affect the duplicate page also. How to restrict? Thanks to all
Replies
0
Boosts
0
Views
856
Activity
Oct ’23
PencilKit: zoom PKCanvasView, drawing blurred (swift, iOS 15)
It seems PKCanvasView overrides the property UIScrollViewDelegate which inherits from the UIScrollView to PKCanvasViewDelegate. And does not provide access to UIScrollViewDelegate. In order to implement zooming, so I added a PKCanvasView into my own UIScrollView. And implemented delegate method viewForZooming in which return PKCanvasView. But all drawing in PKCanvasView was blurred when zooming or scale. How to re-render drawing after zoom to make it has reasonable stroke width and clear ? Some related code: let canvasView = PKCanvasView() let scrollView = UIScrollView() override func viewDidLoad() {     super.viewDidLoad()     self.view.addSubview(scrollView)     scrollView.addSubview(canvasView)     scrollView.delegate = self     scrollView.minimumZoomScale = 0.5     scrollView.maximumZoomScale = 2.5 } func viewForZooming(in scrollView: UIScrollView) -> UIView? {     return canvasView } Some solutions I had tried: 1: Reset PKCanvasView contentScaleFactor func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {     if let canvas = view {       let contentScale = scale * UIScreen.main.scale       canvas.contentScaleFactor = contentScale     }   } Not worked! 2: Re-render PKStroke: func reRender(_ scale: CGFloat) {     let newStrokeWidth = strokeWidth * scale     var newDrawingStrokes: [PKStroke] = []     for stroke in canvasView.drawing.strokes {       canvasView.tool = PKInkingTool(.pen, color: .red, width: newStrokeWidth)       var newPoints = [PKStrokePoint]()       stroke.path.forEach { (point) in         let newPoint = PKStrokePoint(location: point.location,                        timeOffset: point.timeOffset,                        size: CGSize(width: newStrokeWidth, height: newStrokeWidth),                        opacity: CGFloat(1), force: point.force,                        azimuth: point.azimuth, altitude: point.altitude)         newPoints.append(newPoint)       }       let newPath = PKStrokePath(controlPoints: newPoints, creationDate: Date())       let newStroke = PKStroke(ink: PKInk(.pen, color: UIColor.red), path: newPath)       newDrawingStrokes.append(newStroke)     }     let newDrawing = PKDrawing(strokes: newDrawingStrokes)     canvasView.drawing = newDrawing   } Not worked! Still blurred, just changed strokeWidth by multiply scale. 3: I try to reset PKDrawing or PKStroke transform by using scrollView scale. Then PKDrawing position disordered and it was still blurred. Please help me.
Replies
5
Boosts
4
Views
5.8k
Activity
Oct ’23
Swift PKLassoTool menu
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.
Replies
0
Boosts
1
Views
711
Activity
Oct ’23
Severe lag adding PKStroke to PKDrawing
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.
Replies
0
Boosts
1
Views
799
Activity
Oct ’23
Remove "Insert Space Above" option from PkCanvasView
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?
Replies
2
Boosts
2
Views
1.6k
Activity
Oct ’23
PKCanvasView no longer shows edit (paste) menu on long press (iOS 16.1)
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.
Replies
7
Boosts
1
Views
3.5k
Activity
Oct ’23
Swift PKLassoTool custom menu
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.
Replies
2
Boosts
3
Views
985
Activity
Sep ’23
Can't load/decode PKDrawing using any of the new inks (version 2, iOS 17)
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.
Replies
1
Boosts
3
Views
1.4k
Activity
Aug ’23
PencilKit's delegate method being called when used .popover style
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.
Replies
1
Boosts
0
Views
962
Activity
Aug ’23
wwdc2022-10089 issue
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
Replies
2
Boosts
0
Views
1.2k
Activity
Jul ’23
Simultaneously interacting with SwiftUI View below a UIViewRepresentable
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!
Replies
3
Boosts
2
Views
2.9k
Activity
Jul ’23
Making a Freeform board style view
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
Replies
0
Boosts
1
Views
1.2k
Activity
Jun ’23
Laggy PencilKit stroke response
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?
Replies
1
Boosts
1
Views
1.3k
Activity
May ’23
What is a proper way to scale PKCanvasView (UIScrollView)?
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.
Replies
0
Boosts
0
Views
1.2k
Activity
Apr ’23
How to create .drawing file for PencilKit?
I would like to ask if I have the following code : import PencilKit import SwiftUI struct ContentView : View{ var canvas = PKCanvasView() var picker = PKToolPicker() var body: some View{ MyPencilView(canvas : canvas, picker: picker) } } struct MyPencilView: UIViewRepresentable{ var canvas: PKCanvasView let picker: PKToolPicker func makeUIView(context: Context) -> some UIView { picker.setVisible(true, forFirstResponder: canvas) picker.addObserver(canvas) canvas.becomeFirstResponder() return canvas } func updateUIView(_ uiView: UIViewType, context: Context) { } } Where should I add dataRapresentation() to save the . drawing file in the asset folder? Thanks in advance
Replies
1
Boosts
0
Views
1.3k
Activity
Apr ’23
touch.force unit - collecting data from Apple Pencil
Hello, I am currently working in an Alzheimer's related researching where we are utilizing the force (pressure) data from the Apple Pencil.  While we are able to get a range of the force values (0 - 4.2), we do not know the actual unit of this pressure. I have gone through the documentation but I did not find any mention about the unit of the touch.force parameter.  What is the unit of this touch.force parameter?  I also wanted to ask about being able to capture other information from the apple pencil like accelerometer data, gyroscope data etc. The more data we are able to capture from the pencil the better, so that the data can be used for future studies about Alzheimer's Disease. Currently I am using the pencil to capture the following data: - Force Location (coordinates) Altitude angle Azimuth angle Azimuth Unit Vector Perpendicular Force (calculated as per documentation) Major Radius Tap Count If there is anything more that I can capture via the apple pencil, it would be great for future studies.  Additionally, a little about the application, we are having users draw a clock on a canvas and collecting that data. I am using a PKCanvasView for the canvas, and if there is any data that I can collect via the canvas, I would love to know that as well. As of now, I am using the PKCanvasViewDelegate to get data, but if there is anything more that I can collect, that would be great.  Thank you for your time
Replies
0
Boosts
0
Views
941
Activity
Mar ’23
How to create multiple layers in PKCanvasView?
How can I use PKCanvasView in PencilKit to create multiple layers? These views are superimposed on each other. I want the drawing of each layer is kept independent from each other. It's also important that when the user uses the drag or zoom gesture on it, all layers MUST be scaled or dragged at the same time. Please provide a more concise, efficient and appropriate code implementation. Thanks.
Replies
0
Boosts
1
Views
1.3k
Activity
Mar ’23
How to increase the width of PencilKit .pen stroke?
The default PKInkingTool with inkType .pen seems to be limited in its stroke width. With a maximum width of around 25.66. Why is that? How can I increase it? Example code: canvasView.tool = PKInkingTool(.pen, color: .black, width: 10) print(canvasView.tool) //Prints: PKInkingTool(tool: <PKInkingTool: 0x2837ba140 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=10.000000>) canvasView.tool = PKInkingTool(.pen, color: .black, width: 20) //Prints: PKInkingTool(tool: <PKInkingTool: 0x282730140 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=20.000000>) //Problem starts here: canvasView.tool = PKInkingTool(.pen, color: .black, width: 30) //Prints: PKInkingTool(tool: <PKInkingTool: 0x28077a0c0 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=25.659260>) canvasView.tool = PKInkingTool(.pen, color: .black, width: 100) //Prints: PKInkingTool(tool: <PKInkingTool: 0x2824e08e0 com.apple.ink.pen color=UIExtendedSRGBColorSpace 0 0 0 1 width=25.659260>) How can I increase the stroke width? 🤔
Replies
2
Boosts
0
Views
1.4k
Activity
Feb ’23