Article

Handling Double Taps from Apple Pencil

Detect and respond to double taps a user makes on Apple Pencil.

Overview

You can use Pencil interactions to allow users to access functionality in your app quickly. Double-tapping the Apple Pencil lets the user perform actions such as switching between drawing tools without moving the Pencil to another location on the screen.

Add a Pencil Interaction

To handle double taps from Apple Pencil in your app, create a UIPencilInteraction object and set its delegate to an object that implements the UIPencilInteractionDelegate protocol. Finally, add the interaction to your app’s view, as in Listing 1.

Listing 1

Adding a Pencil interaction to a view

let pencilInteraction = UIPencilInteraction()
pencilInteraction.delegate = self
view.addInteraction(pencilInteraction)

Handle the Double Tap

The delegate receives the double tap event when the user double-taps their Pencil. It implements the pencilInteractionDidTap(_:) method, which the system calls after the user double-taps the Apple Pencil.

In your implementation of this method, decide whether to perform the action selected by the user in the Settings app, or perform an alternative behavior that’s suitable for your app. When possible, perform the user’s preferred action.

Taking this step allows you to provide a consistent user experience across apps that support Pencil interactions. To check the user’s preferred action, use the preferredTapAction class property on UIPencilInteraction, as in Listing 2.

Listing 2

Switching drawing tools after double-tapping an Apple Pencil

func pencilInteractionDidTap(_ interaction: UIPencilInteraction) {
    if UIPencilInteraction.preferredTapAction == .switchPrevious {
        leftRingControl.switchToPreviousTool()
    }
}

Should you decide not to support the preferred action, provide an intuitive way to let the user know what will happen when they double-tap the Pencil, and give them the option to disable this action.

See Also

First Steps

Handling Input from Apple Pencil

Learn how to detect and respond to touches from Apple Pencil.