Apple Pencil Double-Tap Actions

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 PSPDFAnnotationToolbar), 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.

Default Behavior

Apple Pencil double-tap actions are mostly handled by PSPDFAnnotationStateManager. It has a pencilInteraction property, which is an instance of UIPencilInteraction.

All cases of UIPencilPreferredAction enum are handled:

  • 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-nil state, or to nil if the previous state was set twice in a row.
  • showColorPalette — Calls annotationStateManagerDidRequestShowingColorPalette: on all delegates. PSPDFAnnotationToolbar implements this by showing the annotation inspector.

Custom Tools UI

If you use a custom UI instead of PSPDFAnnotationToolbar, and if the UI uses PSPDFAnnotationStateManager, 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 showColorPalette UIPencilPreferredAction, you must provide a state manager delegate and implement the PSPDFAnnotationStateManagerDelegate method annotationStateManagerDidRequestShowingColorPalette:. The implementation would typically call toggleStylePicker:presentationOptions: on the state manager, unless you want to show a custom color/style picker. PSPDFAnnotationToolbar’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 isEnabled property.

Custom Actions

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 sets its delegate to your own object. You can see this in action in the CustomPencilInteractionActionExample in the Catalog example app.