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

137 Posts

Post

Replies

Boosts

Views

Activity

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
688
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.2k
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
827
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.3k
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
805
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
896
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.7k
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.1k
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.1k
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
1k
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.1k
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
826
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.1k
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.2k
Feb ’23
PencilKit new tools only available for some developers
Hello, I was trying to use the new Pencilkit tools watercolor, monoline fountainpen but there is no way to implement them in Xcode. There is even a Link to Apples documentation but it's not accessible in Xcode somehow. https://developer.apple.com/documentation/pencilkit/pkinkingtool/inktype/monoline?language=o_9 First I thought they did the same thing like back in the days where they added the shape recognition feature and then removed it after a few weeks. !!But it looks like as if some apps have access to this new tools for example PRODRAFTS ... How is that possible ?? !!
1
2
1.3k
Jan ’23
pencil kit issue--drawing can't stay on canvas
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) { } }
1
1
1.2k
Dec ’22
PencilKit '.monoline' as a PKInkingTool.InkType suddenly doesn't work anymore
Hello Guys, I am writing a program using PencilKit. Yesterday, everything worked fine with this line: @State var pencilStyle: PKInkingTool.InkType = .monoline But today, i am getting this Error: Type 'PKInkingTool.InkType' has no member 'monoline'" When I started Xcode Today, it downloaded a new Simulator for iOS 16.2. How can I fix this issue? I can't use my Projekt without fixing this... Thanks in advance <3
0
1
938
Dec ’22
Why doesn't PKStroke conform to Hashable?
I want to work directly with PKStrokes in my app. To transform and change them individually. By one or by grouping them. I thought a perfect way of keeping track of them is to put them in a Set<PKStroke>. By doing this I receive an error: "Type 'PKStroke' does not conform to protocol 'Hashable'". Correct me if I'm wrong, but aren't all PKStrokes unique? Thus they should be eligible to be kept in a Set? What is a correct way of accessing/grouping/storing PKStrokes then? Manually searching for them in canvasView.drawing.strokes? Thank you very much!
0
1
871
Dec ’22
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
688
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.5k
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.2k
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
827
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.3k
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
805
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
896
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.7k
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.1k
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.1k
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
1k
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.1k
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
826
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.1k
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.2k
Activity
Feb ’23
PencilKit new tools only available for some developers
Hello, I was trying to use the new Pencilkit tools watercolor, monoline fountainpen but there is no way to implement them in Xcode. There is even a Link to Apples documentation but it's not accessible in Xcode somehow. https://developer.apple.com/documentation/pencilkit/pkinkingtool/inktype/monoline?language=o_9 First I thought they did the same thing like back in the days where they added the shape recognition feature and then removed it after a few weeks. !!But it looks like as if some apps have access to this new tools for example PRODRAFTS ... How is that possible ?? !!
Replies
1
Boosts
2
Views
1.3k
Activity
Jan ’23
pencil kit issue--drawing can't stay on canvas
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) { } }
Replies
1
Boosts
1
Views
1.2k
Activity
Dec ’22
PencilKit '.monoline' as a PKInkingTool.InkType suddenly doesn't work anymore
Hello Guys, I am writing a program using PencilKit. Yesterday, everything worked fine with this line: @State var pencilStyle: PKInkingTool.InkType = .monoline But today, i am getting this Error: Type 'PKInkingTool.InkType' has no member 'monoline'" When I started Xcode Today, it downloaded a new Simulator for iOS 16.2. How can I fix this issue? I can't use my Projekt without fixing this... Thanks in advance <3
Replies
0
Boosts
1
Views
938
Activity
Dec ’22
Why doesn't PKStroke conform to Hashable?
I want to work directly with PKStrokes in my app. To transform and change them individually. By one or by grouping them. I thought a perfect way of keeping track of them is to put them in a Set<PKStroke>. By doing this I receive an error: "Type 'PKStroke' does not conform to protocol 'Hashable'". Correct me if I'm wrong, but aren't all PKStrokes unique? Thus they should be eligible to be kept in a Set? What is a correct way of accessing/grouping/storing PKStrokes then? Manually searching for them in canvasView.drawing.strokes? Thank you very much!
Replies
0
Boosts
1
Views
871
Activity
Dec ’22
How to allowsFingerDrawing using PencilKit
Xcode showing me warning 'allowsFingerDrawing' was deprecated in iOS 14.0: Use 'drawingPolicy' property.
Replies
1
Boosts
0
Views
884
Activity
Dec ’22