Hello,
The Apple Pencil Pro brought with it the UICanvasFeedbackGenerator API, which lets us trigger haptic feedback on discrete events initiated by the pencil. That works fine.
My question then: is it possible / are we "allowed" to trigger haptic feedback on events that weren't initiated by the pencil?
For example, say the user is using a left hand finger to drag a slider, while holding the pencil in their right hand-- would it be possible to make the pencil vibrate to indicate the dragged slider knob reached a certain point?
Or is the rule that vibration is only possible/allowed when the pencil itself generated a touch?
Thanks!
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
18 Posts
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I’ve been testing out PaperKit from beta 1 up until 3, then took a break and on my return at beta 7 I find that the .drawing property of the PaperMarkup data model has been removed, i.e. a PKDrawing can no longer be added / modified on PaperKit. Also, the shape recognition feature seems to have been removed.
I see this as a tremendous drawback. I filed feedback already: FB19893338
Please bring it back.
By setting the PKCanvasView background color to blue, I can tell that the PKCanvasView for each PDFPage is created normally, but it does not respond to touch. Specifically, whether it is finger or applepencil, all the responses of the page occur from PDFView(such as zoom and scroll), and PKCanvasView can not draw, please how to solve?
class PDFAnnotatableViewController: UIViewController, PDFViewDelegate {
private let pdfView = PDFView()
private var pdfDocument: PDFDocument?
let file: FileItem
private var userSettings: UserSettings
@Binding var selectedPage: Int
@Binding var currentMode: Mode
@Binding var latestPdfChatResponse: LatestPDFChatResponse
@State private var pdfPageCoordinator = PDFPageCoordinator()
@ObservedObject var userMessage: ChatMessage
init(file: FileItem,
userSettings: UserSettings,
drawDataList: Binding<[DrawDataItem]>,
selectedPage: Binding<Int>,
currentMode: Binding<Mode>,
latestPdfChatResponse: Binding<LatestPDFChatResponse>,
userMessage: ChatMessage) {
self.file = file
self.userSettings = userSettings
self._selectedPage = selectedPage
self._currentMode = currentMode
self._latestPdfChatResponse = latestPdfChatResponse
self.userMessage = userMessage
super.init(nibName: nil, bundle: nil)
DispatchQueue.global(qos: .userInitiated).async {
if let document = PDFDocument(url: file.pdfLocalUrl) {
DispatchQueue.main.async {
self.pdfDocument = document
self.pdfView.document = document
self.goToPage(selectedPage: selectedPage.wrappedValue - 1)
}
}
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setupPDFView()
}
private func setupPDFView() {
pdfView.delegate = self
pdfView.autoScales = true
pdfView.displayMode = .singlePage
pdfView.displayDirection = .vertical
pdfView.backgroundColor = .white
pdfView.usePageViewController(true)
pdfView.displaysPageBreaks = false
pdfView.displaysAsBook = false
pdfView.minScaleFactor = 0.8
pdfView.maxScaleFactor = 3.5
pdfView.pageOverlayViewProvider = pdfPageCoordinator
if let document = pdfDocument {
pdfView.document = document
goToPage(selectedPage: selectedPage)
}
pdfView.frame = view.bounds
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(pdfView)
NotificationCenter.default.addObserver(
self,
selector: #selector(handlePageChange),
name: .PDFViewPageChanged,
object: pdfView
)
}
// Dealing with page turning
@objc private func handlePageChange(notification: Notification) {
guard let currentPage = pdfView.currentPage, let document = pdfView.document else { return }
let currentPageIndex = document.index(for: currentPage)
if currentPageIndex != selectedPage - 1 {
DispatchQueue.main.async {
self.selectedPage = currentPageIndex + 1
}
}
}
func goToPage(selectedPage: Int) {
guard let document = pdfView.document else { return }
if let page = document.page(at: selectedPage) {
pdfView.go(to: page)
}
}
// Switch function
func togglecurrentMode(currentMode: Mode){
DispatchQueue.main.async {
if self.currentMode == .none{
self.pdfView.usePageViewController(true)
self.pdfView.isUserInteractionEnabled = true
} else if self.currentMode == .annotation {
if let page = self.pdfView.currentPage {
if let canvasView = self.pdfPageCoordinator.getCanvasView(forPage: page) {
canvasView.isUserInteractionEnabled = true
canvasView.tool = PKInkingTool(.pen, color: .red, width: 20)
canvasView.drawingPolicy = .anyInput
canvasView.setNeedsDisplay()
}
}
}
}
}
}
class MyPDFPage: PDFPage {
var drawing: PKDrawing?
func setDrawing(_ drawing: PKDrawing) {
self.drawing = drawing
}
func getDrawing() -> PKDrawing? {
return self.drawing
}
}
class PDFPageCoordinator: NSObject, PDFPageOverlayViewProvider {
var pageToViewMapping = [PDFPage: PKCanvasView]()
func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? {
var resultView: PKCanvasView? = nil
if let overlayView = pageToViewMapping[page] {
resultView = overlayView
} else {
let canvasView = PKCanvasView(frame: view.bounds)
canvasView.drawingPolicy = .anyInput
canvasView.tool = PKInkingTool(.pen, color: .systemYellow, width: 20)
canvasView.backgroundColor = .blue
pageToViewMapping[page] = canvasView
resultView = canvasView
}
if let page = page as? MyPDFPage, let drawing = page.drawing {
resultView?.drawing = drawing
}
return resultView
}
func pdfView(_ pdfView: PDFView, willEndDisplayingOverlayView overlayView: UIView, for page: PDFPage) {
guard let overlayView = overlayView as? PKCanvasView, let page = page as? MyPDFPage else { return }
page.drawing = overlayView.drawing
pageToViewMapping.removeValue(forKey: page)
}
func savePDFDocument(_ pdfDocument: PDFDocument) -> Data {
for i in 0..<pdfDocument.pageCount {
if let page = pdfDocument.page(at: i) as? MyPDFPage, let drawing = page.drawing {
let newAnnotation = PDFAnnotation(bounds: drawing.bounds, forType: .stamp, withProperties: nil)
let codedData = try! NSKeyedArchiver.archivedData(withRootObject: drawing, requiringSecureCoding: true)
newAnnotation.setValue(codedData, forAnnotationKey: PDFAnnotationKey(rawValue: "drawingData"))
page.addAnnotation(newAnnotation)
}
}
let options = [PDFDocumentWriteOption.burnInAnnotationsOption: true]
if let resultData = pdfDocument.dataRepresentation(options: options) {
return resultData
}
return Data()
}
func getCanvasView(forPage page: PDFPage) -> PKCanvasView? {
return pageToViewMapping[page]
}
}
Is there an error in my code? Please tell me how to make PKCanvasView painting normally?
I have a new M4 iPad Air and a brand new Apple Pencil Pro. It was working well until I installed iPadOS 26.0 (23A5297m) beta. Now, when using GoodNotes to write the Pencil functions for about 6-8 seconds and then disconnects for 2-3 seconds and then reconnects. You can imagine how disruptive this is. It does the same thing trying to highlight in the Kindle app, so I think it is an iOS issue.
As I write this, I am discovering that the Pencil is not wanting to stay connected at all despite showing up in the list of Bluetooth devices and connecting temporarily. I've tried forgetting and reconnecting the Pencil several times. It is charged to 100%, and I receive the alert that the iPad detects the Pencil.
Is anyone else having this issue? Do I need to assume this is being addressed?
Hi everyone 👋
I’ve been using the Apple Pencil Pro with my iPad Pro M4 and absolutely love it — the squeeze gesture, rotation, and haptics are amazing for creative work. But I’ve run into a little roadblock…
Right now, only one Pencil Pro can be paired at a time. So while one is charging, you can’t use another as a backup without unpairing and re-pairing, which interrupts the workflow.
I’d really love to see one of two things:
The ability to use one Pencil while another charges
or
An official external charger (or support for third-party ones)
Personally, I’d happily buy both a second Pencil and a charger if this became possible. I’ve even chatted with other creatives who feel the same — it would make a huge difference for long projects or working on the go.
Just wanted to share this idea and see if anyone else here would like this too. Thanks for reading!
I added a canvas view using PDFPageOverlayViewProvider. When I zoom the PDFView, the drawing is scaled, but its quality becomes blurry. How can I fix this?
import SwiftUI
import PDFKit
import PencilKit
import CoreGraphics
struct ContentView: View {
var body: some View {
if
let url = Bundle.main.url(forResource: "sample", withExtension: "pdf"),
let data = try? Data(contentsOf: url),
let document = PDFDocument(data: data)
{
PDFRepresentableView(document: document)
} else {
Text("fail")
}
}
}
#Preview {
ContentView()
}
struct PDFRepresentableView: UIViewRepresentable {
let document: PDFDocument
let pdfView = PDFView()
func makeUIView(context: Context) -> PDFView {
pdfView.displayMode = .singlePageContinuous
pdfView.usePageViewController(false)
pdfView.displayDirection = .vertical
pdfView.pageOverlayViewProvider = context.coordinator
pdfView.document = document
pdfView.autoScales = false
pdfView.minScaleFactor = 0.7
pdfView.maxScaleFactor = 4
return pdfView
}
func updateUIView(_ uiView: PDFView, context: Context) {
// Optional: update logic if needed
}
func makeCoordinator() -> CustomCoordinator {
return CustomCoordinator(parent: self)
}
}
class CustomCoordinator: NSObject, PDFPageOverlayViewProvider, PKCanvasViewDelegate {
let parent: PDFRepresentableView
init(parent: PDFRepresentableView) {
self.parent = parent
}
func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? {
let result = UIView()
let canvasView = PKCanvasView()
canvasView.drawingPolicy = .anyInput
canvasView.tool = PKInkingTool(.pen, color: .blue, width: 20)
canvasView.translatesAutoresizingMaskIntoConstraints = false
result.addSubview(canvasView)
NSLayoutConstraint.activate([
canvasView.leadingAnchor.constraint(equalTo: result.leadingAnchor),
canvasView.trailingAnchor.constraint(equalTo: result.trailingAnchor),
canvasView.topAnchor.constraint(equalTo: result.topAnchor),
canvasView.bottomAnchor.constraint(equalTo: result.bottomAnchor)
])
for subView in view.documentView?.subviews ?? [] {
subView.isUserInteractionEnabled = true
}
result.layoutIfNeeded()
return result
}
}
Hi all,
I’m currently building a SwiftUI app that overlays a PKCanvasView onto each page of a PDFView using PDFPageOverlayViewProvider. It works well at the initial scale, but once I zoom into the PDF, the drawings on the PKCanvasView appear blurry or pixelated, even though the PDF itself remains crisp.
I’m trying to adjust canvasView.contentScaleFactor relative to pdfView.scaleFactor to preserve the drawing quality. Here’s a simplified version of the relevant code:
import SwiftUI
import PDFKit
import PencilKit
struct ContentView: View {
var body: some View {
if let url = Bundle.main.url(forResource: "sample", withExtension: "pdf"),
let data = try? Data(contentsOf: url),
let document = PDFDocument(data: data) {
PDFRepresentableView(document: document)
} else {
Text("")
}
}
}
#Preview {
ContentView()
}
struct PDFRepresentableView: UIViewRepresentable {
let document: PDFDocument
let pdfView = PDFView()
func makeUIView(context: Context) -> PDFView {
pdfView.displayMode = .singlePageContinuous
pdfView.usePageViewController(false)
pdfView.displayDirection = .vertical
pdfView.pageOverlayViewProvider = context.coordinator
pdfView.document = document
pdfView.autoScales = false
pdfView.minScaleFactor = 0.7
pdfView.maxScaleFactor = 4
NotificationCenter.default.addObserver(
context.coordinator,
selector: #selector(context.coordinator.onPageZoomAndPan),
name: .PDFViewScaleChanged,
object: pdfView
)
return pdfView
}
func updateUIView(_ uiView: PDFView, context: Context) {
// Optional: update logic if needed
}
func makeCoordinator() -> CustomCoordinator {
return CustomCoordinator(parent: self)
}
}
class CustomCoordinator: NSObject, PDFPageOverlayViewProvider, PKCanvasViewDelegate {
let parent: PDFRepresentableView
init(parent: PDFRepresentableView) {
self.parent = parent
}
func pdfView(_ view: PDFView, overlayViewFor page: PDFPage) -> UIView? {
let canvasView = PKCanvasView()
let rect = page.bounds(for: .mediaBox)
canvasView.drawingPolicy = .anyInput
canvasView.tool = PKInkingTool(.pen, color: .black, width: 10)
canvasView.translatesAutoresizingMaskIntoConstraints = true
canvasView.backgroundColor = .red.withAlphaComponent(0.1)
canvasView.frame = rect
canvasView.isScrollEnabled = false
for subView in view.documentView?.subviews ?? [] {
subView.isUserInteractionEnabled = true
}
return canvasView
}
@objc func onPageZoomAndPan() {
parent.pdfView.documentView?.subviews.forEach { subview in
if
subview.theClassName == "PDFPageView",
let pageViewPrivate = subview.value(forKey: "_private") as? NSObject,
let page = pageViewPrivate.value(forKey: "page") as? PDFPage {
subview.subviews.forEach { subview in
if let canvasView = subview as? PKCanvasView {
let zoomScale = parent.pdfView.scaleFactor
canvasView.contentScaleFactor = UIScreen.main.scale * zoomScale
canvasView.drawing = canvasView.drawing
canvasView.setNeedsDisplay()
canvasView.layoutIfNeeded()
}
}
}
}
print("Zoom changed. Current scale: \(parent.pdfView.scaleFactor)")
}
}
extension NSObject {
var theClassName: String {
return NSStringFromClass(type(of: self))
}
}
But this doesn’t seem to improve the rendered quality. The lines still appear blurry when zoomed in.
What I’ve tried:
• Adjusting contentScaleFactor and forcing redraw
• Reassigning canvasView.drawing
• Calling setNeedsDisplay() and layoutIfNeeded()
None of these approaches seem to re-render the canvas at a higher resolution or match the zoomed scale of the PDF.
My questions:
1. Is there a correct way to scale PKCanvasView content to match PDF zoom levels?
2. Should I recreate the canvas or drawing when zoom changes?
3. Is PKCanvasView just not intended to handle high zoom fidelity?
If anyone has successfully overlaid high-resolution canvas drawing on a zoomable PDFView, I’d love to hear how you managed it.
Thanks in advance!
The canvasView in PKCanvasViewDelegate only holds the drawing data up to the most recently completed stroke. I want to access the currently in-progress drawing (before the pencil is lifted) in order to measure the path’s size — but I can’t find a way to do it.
Is there any way you could help me with this?
I got 3 PKCanvasView, 2 below the Major one.
Users draw lines on the top one, then sync the last stroke to the one underneath. If the stroke crosses two PKCanvasView, we replace the stroke with a bezier curve.
If a stroke doesn't cross regions, we sync the stroke to the one below it(B) as if it is drawn directly on B. The problem is if the B is inside a UIScrollview with a zoom scale not 1, the stroke from major to B will shrink or grow. Does anybody have a solution for this please?
What I did
Also put the major canvas into a uiScrollview, and make sure the zoomScale is the same as the B.
for scale >=1, it works as expected, for scale < 1, sometimes it works, sometimes it doesn't. for example, 0.5, 0.6, 0.8, 0.5 doesn't work, 0.6, 0.8 works, I don't know why.
What it costs
It cost me 16*4 hours for these days. I didn't find a solution. Hopefully, some one can solve it.
In my PKCanvasView, I want to center the drawing within the visible area, which I have been able to do except for one thing: I want to take into account the portion covered by the tool picker (on iPhone). I figured I could do this using the toolPickerFramesObscuredDidChange() callback on the delegate, but it never seems to get called, while toolPickerVisibilityDidChange() gets called as expected.
I tried out the PencilKitDraw sample app from Apple, and saw pretty much the same result. The only time I see a toolPickerFramesObscuredDidChange call is when the device rotates. This doesn't help because I need the initial picker frame before rotating the device, plus my app is portrait only anyway.
So how do I get the tool picker frame if my delegate method isn't getting called?
I’m building a visionOS app that uses PencilKit for drawing. Currently, PencilKit defaults to using the right hand for drawing and the left hand for panning, with no apparent way to change this behavior.
Some of my users are left-handed, and they naturally want to draw with their left hand and pan with their right. However, PencilKit doesn’t seem to support this interaction pattern.
Is there a way to customize which hand does what in PencilKit on visionOS?
Or have I missed some API or workaround that would allow support for left-handed users?
I want to create a brush similar to a fountain pen, with a three-dimensional feel to the strokes and a distinct tip. Alternatively, is it possible to achieve this by modifying the configuration parameters of a fountain pen brush?
I want to create a brush similar to a fountain pen, with a three-dimensional feel to the strokes and a distinct tip. Alternatively, is it possible to achieve this by modifying the configuration parameters of a fountain pen brush?
There are use cases where someone who's using an Apple Pencil may not want to enter text via Scribble. A simple example is writing "UIViewController" in a text view is unlikely to be successful.
I'd like to disable Scribble in this case and let the keyboard become the input mechanism. (Disabling Scribble system-wide in Settings is both cumbersome and overkill.)
The closest I can come to making this happen is by adding a UIScribbleInteraction on a UITextView and returning false when scribbleInteraction(shouldBeginAt:) is called.
This disables Scribble on the text view, and prevents writing from being converted into text, but the input widget still appears on screen and isn't very useful.
Here is a sample project that demonstrates the problem:
http://files.iconfactory.net/craig/bugs/Scribbler.zip
Hopefully, I'm doing something wrong here. If not, I'm happy to submit this as a FB.
-ch
When a custom tool items set is formed in PKToolPicker with each inking item having non-nil identifier created using PKToolPickerInkingItem(type: ,color:,width:,identifier: ), changing color or width of an inking item (pen, pencil etc) causes an instant crash.
I believe it is a bug in PencilKit.
It seems that when you change the color or width of an inking item having a identifier in squeeze tool palette, it tries to find a tool item without identifier (the default tool picker has items without identifier) in the tool items set. I guess it cannot find, thus the find function returns either -1 or highest integer number (2^63 -1 ) and it uses this number as index without boundary checking. That's why we observe [__NSArrayM replaceObjectAtIndex:withObject:]: index 9223372036854775807 beyond bounds [0 .. 9]
I filed a report on Feedback Assistant with id: FB15519801 too.
The corresponding part in crash report is as follows:
0 CoreFoundation 0x183e0908c __exceptionPreprocess + 164 (NSException.m:249)
1 libobjc.A.dylib 0x18110b2e4 objc_exception_throw + 88 (objc-exception.mm:356)
2 CoreFoundation 0x183de4048 -[__NSArrayM replaceObjectAtIndex:withObject:] + 1020 (NSArrayM.m:180)
3 PencilKit 0x1c44f73c8 -[PKToolPicker _setSelectedTool:saveState:updateUI:updateLastSelectedTool:] + 800
(PKToolPicker.m:587)
4 PencilKit 0x1c45a5684 -[PKPencilSqueezeControllerPaletteViewDelegateProxy paletteView:didSelectTool:atIndex:] + 200 (PKPencilSqueezeControllerPaletteViewDelegateProxy.m:227)
5 PencilKit 0x1c460906c -[PKSqueezePaletteView _didSelectTool:atIndex:] + 196 (PKSqueezePaletteView.m:441)
6 PencilKit 0x1c462203c -[PKSqueezePaletteViewExpandedInkingToolLayout _didTapStrokeWeightButton:] + 336
(PKSqueezePaletteViewExpandedInkingToolLayout.m:224)
7 UIKitCore 0x18691edd8 -[UIApplication sendAction:to:from:forEvent:] + 100 (UIApplication.m:5797)
8 UIKitCore 0x18691ecb0 -[UIControl sendAction:to:forEvent:] + 112 (UIControl.m:942)
9 UIKitCore 0x18691eb00 -[UIControl _sendActionsForEvents:withEvent:] + 324 (UIControl.m:1013)
10 UIKitCore 0x187080568 -[UIButton _sendActionsForEvents:withEvent:] + 124 (UIButton.m:4192)
11 UIKitCore 0x187081d7c -[UIControl touchesEnded:withEvent:] + 400 (UIControl.m:692)
12 UIKitCore 0x1868675b0 -[UIWindow _sendTouchesForEvent:] + 852 (UIWindow.m:3313)
and the exception reason is
*** -[__NSArrayM replaceObjectAtIndex:withObject:]: index 9223372036854775807 beyond bounds [0 .. 9]
I am creating an application using PKCanvasview.
One function of that app is the ability to trace and retrieve a UITextView that has been addSubviewed to a PKCanvasView.
We have confirmed that this functionality works correctly in the simulator and on the actual device on IOS 17.4.
This feature did not work correctly on the IOS18 simulator and the actual device.
Is this a bug?
And if it is normal behavior, is there an alternative?
There is a significant rendering issue in PencilKit when using an iPad set to "More Space" display mode, combined with an Apple Pencil that supports hover functionality (e.g., Apple Pencil 2). This problem affects all applications that use PencilKit, including Apple's own Notes and Quick Note. The issue results in flickering black borders and subtle jittering while drawing, which is especially noticeable during tasks requiring precise handwriting, such as writing mathematical expressions. Due to the short strokes and frequent lifts and drops of the pencil, the jitter is much more pronounced, leading to visual discomfort and even dizziness after extended use.
Steps to Reproduce:
Open Settings on your iPad.
Navigate to Display & Brightness > Display Zoom > More Space.
Switch to the More Space display mode.
Open the Notes app or trigger Quick Note from any application by swiping from the bottom-right corner.
Start drawing or writing using the Apple Pencil (with hover functionality) in the writing area.
Observe the display anomalies as you hover and write:
Flickering black borders appear intermittently around the writing area.
The strokes show subtle jittering whenever you lift and lower the pencil. This is particularly disruptive when writing short, precise strokes, such as those in mathematical expressions, where the frequent up-and-down motion makes the jitter more apparent.
Expected Results:
Smooth and stable drawing experience with no visual artifacts or jittering during interactions with the Apple Pencil, regardless of the display zoom settings.
Actual Results:
Flickering black borders intermittently appear during drawing.
Jittering of strokes is noticeable, particularly when lifting and lowering the Apple Pencil for short strokes. This disrupts precise writing tasks, especially in cases like mathematical notation, where frequent pen lifts and short strokes make the jitter much more apparent. This can lead to discomfort (e.g., dizziness) after extended use.
System-Wide Impact:
This issue affects all apps that utilize PencilKit, including third-party apps such as Doodle Draw (link). Since PencilKit is a core framework, the rendering bug significantly degrades the user experience across multiple productivity and note-taking applications.
Similar Reports:
There are numerous discussions about this issue online, indicating that many users are experiencing the same problem.
Reddit Discussion: https://www.reddit.com/r/ipad/comments/1f042ol/comment/ljvilv6/
Apple Support Thread
Additionally, a feedback report (ID: FB15166022) related to this issue with Notes has been previously filed, but the bug remains unresolved.
This bug severely impacts the usability of PencilKit-enabled apps, especially during tasks that involve frequent, precise pencil strokes, such as writing mathematical expressions. We kindly request the development team investigate this issue promptly to ensure a smooth, stable experience with the iPad's "More Space" display mode and Apple Pencil hover interaction.
I want to add a pkcanvasview image and change its position and size by dragging. And I want to draw a picture using a pencil on top.
The image must be able to change its position at any time, and a picture must be added on top.
If I add it to the subview of pkcanvasview, I cannot do what I want. How can I do this?