Hey, so I am working on a note taking part of my app and I have been working on allowing different inputs. I got the PKCanvas to work on the preview and on the simulator when it is the only thing on the window group. But when I use this View as part of the tool selection in my app, when the user selects the Canvas view it loads but scribbles don't work, only in the preview in Xcode. I don't know why this behavior is happening.
The flow in which the PKCanvasView appears is, user taps NavLink to see their notes, in the NotesView they tap to show the toolbar, toolbar has 4 types of input, text, images, audio, scribbles (all other inputs work accordingly), user taps scribbles the view loads correctly but doesn't allow them to draw.
I have set the drawingPolicy to anyInput
PencilKit
RSS for tagCapture 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
36 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
Hi,
can i create custom tool for PKToolPicker?
On documentation page on PKTool says "Don’t adopt this protocol in your own objects. Instead, create a tool object to provide users with the desired the tool behavior."
Best regards,
Matej Klemen
on iOS, I want to add up undo/redo and a close button. On ipadOS, I only need to add a close button
What’s your experience in adding a close button to the ToolPicker? Or at least have the position of the window so I can add an overlapping box (even on floating).
Has anyone got PKToolPicker appearing in an iPad app running in compatibility mode on Vision Pro?
In our app, it appears fine on iPad, but not in the Vision Pro simulator.
Even though the tools do not appear, I am able to draw in the canvas (...though not change the pen, of course).
I did not read anywhere that it was not supported on Vision Pro. I only saw that interactions with pencil do not work, but that other forms of interaction (eg drawing with touch) should work.
Anyone seen it working on Vision Pro?
In the recent WWDC 2022, FreeForm app was introduced and in that app in the tool picker there was a Fill Tool.
I want to put that fill tool (or any similar filling tool) in my xcode app that I am building. But when I use pencilkit PKToolPicker, there is not Fill Tool.
So how to get this fill tool in my app.
Any leads would be helpful. Thanks.
When I display the PKToolPicker in my app it looks similar to the image below
If you use one of the apps from Apple like Pages or Freeform you see a picker similar to this one.
Notice that the PKToolPicker from the Apple app has 6 tools not including the ruler. With the tool all the way to the left being the writing tool.
How do you get the picker to display with the writing tool? I have looked over the API and I can see where you can set if the ruler is displayed or not but I can't find anything for the writing tool
Dear All,
I am currently facing a challenge with updating the contentScaleFactor of my PKCanvasView, which is embedded within a custom UIScrollView. I have configured the PKCanvasView to have both its maximum and minimum zoomScale set to 1.0, ensuring that users can only zoom into the UIScrollView and not directly into the PKCanvasView.
Upon the completion of a zoom action in my UIScrollView, a notification is published and received by the PKCanvasView. This triggers a function intended to update the contentScaleFactor. However, this process is not functioning as expected.
Despite consulting with several engineers at WWDC24 and exploring multiple potential solutions, the issue remains unresolved.
Interestingly, when zooming into a PDF in the Documents app on iOS 17, the strokes are re-rendered perfectly, while in previous iOS versions, this bug persists in the Documents app.
I would greatly appreciate any insights or solutions from those who might have encountered and resolved this issue.
Thank you in advance for your assistance.
Best regards,
Timon
I'm attempting to retrieve the values of the Pencil Pro using the new PencilHoverPose, however according to the documentation it appears these can only be accessed while in a hover state (0 to ~0.5" from the screen).
Is there a way to get the orientation or roll values of the Apple Pencil Pro outside of a hover? I'm attempting to use the pencil as an input device, necessitating it be farther from the screen than 0.5"
Below is a simple view I'm using to display the available values:
import SwiftUI
import UIKit
struct PencilSqueezeView: View {
@State private var squeezeText: String = "Squeeze the Apple Pencil Pro"
@State private var hoverDistance: CGFloat = 0.0
@State private var anchor: UnitPoint = .center
@State private var location: CGPoint = .zero
@State private var altitude: Angle = .degrees(0)
@State private var azimuth: Angle = .degrees(0)
@State private var roll: Angle = .degrees(0)
var body: some View {
VStack {
Text(squeezeText)
.font(.largeTitle)
.padding()
Text("Hover Distance: \(String(format: "%.2f", hoverDistance))")
.font(.title2)
.padding()
Text("Anchor: \(anchor)")
.font(.title2)
.padding()
Text("Location: \(location.x), \(location.y)")
.font(.title2)
.padding()
Text("Altitude: \(altitude.degrees)°")
.font(.title2)
.padding()
Text("Azimuth: \(azimuth.degrees)°")
.font(.title2)
.padding()
Text("Roll: \(roll.degrees)°")
.font(.title2)
.padding()
Spacer()
}
.onPencilSqueeze { phase in
handlePencilSqueeze(phase: phase)
if case let .ended(value) = phase {
if let hoverPose = value.hoverPose {
updateHoverPose(hoverPose)
} else {
print("hoverPose is nil")
}
}
}
.padding()
}
private func handlePencilSqueeze(phase: PencilSqueezeGesturePhase) {
switch phase {
case .active:
squeezeText = "Squeeze began"
case .ended:
squeezeText = "Squeeze ended"
case .failed:
squeezeText = "Squeeze failed"
}
}
private func updateHoverPose(_ hoverPose: PencilHoverPose) {
hoverDistance = hoverPose.zDistance
anchor = hoverPose.anchor
location = hoverPose.location
altitude = hoverPose.altitude
azimuth = hoverPose.azimuth
roll = hoverPose.roll
print("zDistance: \(hoverPose.zDistance)")
print("anchor: \(hoverPose.anchor)")
print("location: \(hoverPose.location)")
print("altitude: \(hoverPose.altitude)")
print("azimuth: \(hoverPose.azimuth)")
print("roll: \(hoverPose.roll)")
}
}
When using PencilKit and the default PKCanvasView, we get a pop-up menu with options like 'Select All | Insert Space' when we long tap on the canvas.
What is the proper way to remove this menu completely?
Thank you for your help.
I wonder if an Apple engineer could confirm: will the Apple Pencil Pro squeeze functionality be detectable in the current API, or will this be a future iPadOS extension to gesture recognizers / UIKit? I’d like to start playing with the functionality if it’s detected behind an existing event though. (Long press?)
DemoCode:
import SwiftUI
import UIKit
import PencilKit
class PencilKitViewController: UIViewController, PKCanvasViewDelegate, PKToolPickerObserver {
lazy var canvasView: PKCanvasView = {
let canvasView = PKCanvasView()
canvasView.drawingPolicy = .anyInput
canvasView.translatesAutoresizingMaskIntoConstraints = false
return canvasView
}()
lazy var toolPicker: PKToolPicker = {
let toolPicker = PKToolPicker()
toolPicker.showsDrawingPolicyControls = true
toolPicker.addObserver(self)
return toolPicker
}()
let drawing = PKDrawing()
override func viewDidLoad() {
super.viewDidLoad()
canvasView.drawing = drawing
canvasView.delegate = self
view.addSubview(canvasView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
canvasView.frame = view.bounds
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
// canvas
func canvasViewDrawingDidChange(_ canvasView: PKCanvasView) {
print("drawing")
}
func canvasViewDidFinishRendering(_ canvasView: PKCanvasView) {
}
func canvasViewDidEndUsingTool(_ canvasView: PKCanvasView) {
}
func canvasViewDidBeginUsingTool(_ canvasView: PKCanvasView) {
}
}
// UIRepresentable for SwiftUI
struct PencilKitView: UIViewControllerRepresentable {
class Coordinator {
var parentObserver: NSKeyValueObservation?
}
var onSubmit: ((UIImage?, Error?) -> Void)? = .none
func makeUIViewController(context: Context) -> PencilKitViewController {
let pencilKitViewController = PencilKitViewController()
context.coordinator.parentObserver = pencilKitViewController.observe(\.parent, changeHandler: { vc, _ in
})
return pencilKitViewController
}
func updateUIViewController(_ uiViewController: PencilKitViewController, context: Context) {
}
func makeCoordinator() -> Self.Coordinator { Coordinator() }
}
struct ContentView: View {
var onSubmit: ((UIImage?, Error?) -> Void)? = .none
var body: some View {
PencilKitView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
iOS:
macCatalyst:
When PKCanvasView is first drawn, existing drawing objects disappear.
PKDrawing, which previously had drawings saved with PKCanvasView, was saved as separate data or file.
After that, while creating a new PKCanvasView, I loaded the saved PKDrawing and reflected it in the new PKCanvasView.
Below is the code.
canvasView!.drawing = draw!
Previously saved text or lines will be displayed normally.
However, when I draw with the pen for the first time (when I touch the screen with the pen), the old writing or lines disappear.
And after I leave a line or text, if I pan across the screen, the old text or line appears again.
If this phenomenon occurs and you touch the screen again with the fan, this phenomenon will no longer occur.
This phenomenon occurs the first time when a new PKCanvasView is declared and the previously saved PKDrawing is reflected.
Could you please help me with why this phenomenon occurs and how to improve it?
Hi, I am trying to make a simple note taking app that users can draw something on pdfview with apple pencil.
(I used PDFKit, PencilKit for the code.)
I followed the instruction code of WWDC22's "What's new in PDFKit." - "overlayProvider"
(so you can see the code at the video.) I was able to draw something each view of pdf page.
But the issue was, the resolution of overlayview or subview of pdfview is low.
As far as I know, the pkcanvasview draws vertor-based drawings. So I never thought the image or the lines I draw will be that blurry.
Is this buggy or is this the normal thing? (+ I added a uibutton as subview of pdfview and the button also looks blurry.)
I even tried to scale up the all the subviews when the subviews' layout is done, using contentScaleFactor.
PKCanvasView inherits UIScrollView, so I enlarged the frame of pkcanvas view and fixed the scale to below 1.0. If the pkcanvasview looks blurry and that is because somewhat zoomed in wrong way, zooming out should be the solution. But, didn't work. Still blurry.
and any other stuff like changing frame or size.
So, anyone having same problem with me, or anyone can give me any solution.
Please help me. I wish this is bug thing that can be fixed in any moment.
-> This image is little bit zoomed in. but the drawing is blurry.
and this is the normal pkcanvasview drawing, just subview of view(of VC).
Hi,
I have an app using PencilKit that works on VisionOS 1.0. It means, a user can pick an inking tool from PKToolPicker and draw on PKCanvasView. The app is now on available on Vision Pro AppStore as well.
However, when I test the app on VisionOS 1.1 RC Simulator, I can pick an inking tool but when I try to draw on the canvas, it just scrolls and no drawing appears on the PKCanvasView.
I also noticed that the VisionOS 1.0 Simulator has the FreeForm app where you can draw with PencilKit but the VisionOS 1.1 RC Simulator does not have the FreeForm app.
Is this a known issue? Will it be fixed before the release or is there a change in API so I can update the app accordingly?
Thanks
Zafer
Running my app in iOS 17 has a weird bug. I can draw as usual on my canvas, but only when I open the color picker from the toolPicker a bunch of weird errors and warnings are posted to the console (I am assuming they are related to it.) Closing the color picker and leaving the screen would normally dismiss the toolbar, but now it stays forever and on every screen. Interestingly the canvas, the tool picker and the viewController are all getting deinitialized, which means there is a second/new toolPicker on screen and I have no reference to it.
Here are the mentioned warnings and errors:
Error acquiring assertion: <Error Domain=RBSServiceErrorDomain Code=1 "(originator doesn't have entitlement com.apple.runningboard.primitiveattribute AND originator doesn't have entitlement com.apple.runningboard.assertions.frontboard AND target is not running or doesn't have entitlement com.apple.runningboard.trustedtarget AND Target not hosted by originator)" UserInfo={NSLocalizedFailureReason=(originator doesn't have entitlement com.apple.runningboard.primitiveattribute AND originator doesn't have entitlement com.apple.runningboard.assertions.frontboard AND target is not running or doesn't have entitlement com.apple.runningboard.trustedtarget AND Target not hosted by originator)}>
Received port for identifier response: <> with error:Error Domain=RBSServiceErrorDomain Code=1 "Client not entitled" UserInfo={RBSEntitlement=com.apple.runningboard.process-state, NSLocalizedFailureReason=Client not entitled, RBSPermanent=false}
elapsedCPUTimeForFrontBoard couldn't generate a task port
Received port for identifier response: <> with error:Error Domain=RBSServiceErrorDomain Code=1 "Client not entitled" UserInfo={RBSEntitlement=com.apple.runningboard.process-state, NSLocalizedFailureReason=Client not entitled, RBSPermanent=false}
elapsedCPUTimeForFrontBoard couldn't generate a task port
Received port for identifier response: <> with error:Error Domain=RBSServiceErrorDomain Code=1 "Client not entitled" UserInfo={RBSEntitlement=com.apple.runningboard.process-state, NSLocalizedFailureReason=Client not entitled, RBSPermanent=false}
elapsedCPUTimeForFrontBoard couldn't generate a task port
No setting found for property named "_UISceneHostingClientSettingsExtension"
No setting found for property named "_UISceneHostingClientSettingsExtension"
No setting found for property named "_UISceneHostingClientSettingsExtension"
A code snippet, but nothing fancy:
private var imageCanvasView = ImageCanvasView()
private var toolPicker = ToolPicker()
override func viewDidLoad() {
super.viewDidLoad()
self.toolPicker.setVisible(true, forFirstResponder: self.imageCanvasView)
self.toolPicker.addObserver(self.imageCanvasView)
self.imageCanvasView.becomeFirstResponder()
}
Any ideas on how to prevent that or at least access/hide the permanent tool picker?
In 2 days we have observed in Crashlytics over 50 crashes related to PKDrawing.image with EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x0000000000000210
So far all on iPads with A10, A12 and A13; 100% on iOS 17 (17.1.1, 17.2, 17.3) (while in others the percent of iOS 17 was around 60-80%).
Context:
Images of the varying frame and scale resulting in screen resolution are being generated sequentially (in a background serial queue called almost one after another when requested), updating one CALayer.contents (and only after this update on Main Thread the next generation is allowed). One zoomable PKCanvasView is present on screen.
The crashing line in code:
let image = drawing.image(from: frame, scale: renderScale).cgImage
The questions:
Is there anything that can be done apart from throttling generation?
Can the circumstances of the crash be determined – are there any indications accessible in code before calling PKDrawing.image that app might crash?
The traces:
0
AGXMetalA12
AGX::BlitContext<AGX::G11::Encoders, AGX::G11::Classes, AGX::G11::ObjClasses>::copyTextureToBuffer(IOGPUMetalResource const*, unsigned long, unsigned long, unsigned long, AGXA12FamilyTexture*, unsigned int, unsigned int, MTLOrigin, MTLSize, unsigned long) + 96
9
PencilKit
PKDrawing.image(from:scale:) + 28
0
AGXMetalA13
<redacted> + 96
9
PencilKit
PKDrawing.image(from:scale:) + 28
0
AGXMetalA10
<redacted> + 72
9
PencilKit
$s9PencilKit9PKDrawingV5image4from5scaleSo7UIImageCSo6CGRectV_12CoreGraphics7CGFloatVtF + 24
Hello!
I've been teaching myself Swift and wanted to challenge myself by creating a drawing app. I want to add a feature that allows the user to share the drawing on their canvas, but I'm having some difficulty. I tried using a ShareLink, but it's asking that I conform in to Transferable. How do I do that?
import SwiftUI
import PencilKit
struct ContentView: View {
@State private var canvasView = PKCanvasView()
var body: some View {
Text("Let's draw!")
GeometryReader { geometry in
VStack {
Spacer()
HStack {
Spacer()
PencilKitView()
.frame(width: geometry.size.width * 1, height: geometry.size.height * 0.7)
Spacer()
}
Spacer()
}
}
//share button
Button("share with a friend") {
let portionRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let scale: CGFloat = 1.0
let image = canvasView.drawing.image(from: portionRect, scale: scale)
ShareLink(
item: image,
preview: SharePreview(
"Share Preview",
image: image
)
)
}
}
}
struct PencilKitView: UIViewRepresentable {
typealias UIViewType = PKCanvasView
let toolPicker = PKToolPicker()
func makeUIView(context: Context) -> PKCanvasView {
let pencilKitCanvasView = PKCanvasView()
pencilKitCanvasView.drawingPolicy = PKCanvasViewDrawingPolicy.anyInput
toolPicker.addObserver(pencilKitCanvasView)
toolPicker.setVisible(true, forFirstResponder: pencilKitCanvasView)
pencilKitCanvasView.becomeFirstResponder()
return pencilKitCanvasView
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
}
}
#Preview {
ContentView()
}
Is there any chance to modify the strokes of PKDrawing while drawing in on-process ? since I notice in canvasViewDrawingDidChange is called after drawing stroke is finished
My objective is to get realtime feedback modification of PKStroke
Thank you in advance
I've noticed in FreeForm app drawing stroke is very nice, like vector object, is there any chance to get the same result of drawing stroke quality like in the FreeForm app ?
I am making a code that uses pencilKit and you can draw on the canvas. However, I want to be able to detect if the resulted drawing is in contact with a Rectangle().
Is there any way to do this?
I dont want to use variables for X and Y positions because I have more than 400 rectangles in a grid.