Support for Apple Pencil Double Tap on iOS
PSPDFKit 8.1 for iOS added built-in support for responding to double-taps on the second-generation Apple Pencil, which is compatible with the third-generation models of iPad Pro.
With PSPDFKit’s default configuration, you don’t need to do anything to enable Apple Pencil actions in your app. If you use a custom tools UI (rather than
AnnotationToolbar), then you need to implement a delegate method for full support. It is possible to disable or replace PSPDFKit’s handling of Apple Pencil actions.
Apple Pencil double-tap actions are mostly handled by
AnnotationStateManager. It has a
pencilInteraction property, which is an instance of
All cases of the
UIPencilPreferredAction enum are handled as follows:
ignore— Does nothing.
switchEraser— If the state manager’s current state supports erasing (only ink), then switch to the eraser. If the current state is the eraser and the previous state supports erasing, then switch to the previous state.
switchPrevious— Switches to the state manager’s previous non-
nilstate, or to
nilif the previous state was set twice in a row.
annotationStateManagerDidRequestShowingColorPalette(_:)on all delegates.
AnnotationToolbarimplements this by showing the annotation inspector.
Custom Tools UI
If you use a custom UI instead of
AnnotationToolbar, and if the UI uses
AnnotationStateManager, then there’s not much more you need to do.
If your UI correctly observes changes to the state manager’s state, then switching to the eraser or previous tool will work automatically.
To support the
UIPencilPreferredActionShowColorPalette, you must provide a state manager delegate and implement
annotationStateManagerDidRequestShowingColorPalette(_:), which is the
AnnotationStateManagerDelegate method. The implementation would typically call
toggleStylePicker(_:presentationOptions:) on the state manager, unless you want to show a custom color/style picker.
AnnotationToolbar’s implementation of this method simply follows the same code path as when its
strokeColorButton is tapped.
If your tools UI can be shown and hidden, be aware that actions will still be handled while the UI is hidden unless you disable the
UIPencilInteraction with its
To perform a custom action in response to Apple Pencil double-tap actions, or to use your own implementation of the standard preferred actions, first you should disable PSPDFKit’s built-in handling. This can be done by removing the annotation state manager’s
pencilInteraction from its view. Setting the
isEnabled property will not work because PSPDFKit may set this property internally.
You can then add your own
UIPencilInteraction to the view hierarchy and set its delegate to your own object. You can see this in action in
CustomPencilInteractionActionExample in the Catalog example app.