TechnologiesSwiftUIPencil Interactions

PencilDoubleTapGestureValue struct

iOSmacOStvOSwatchOSvisionOS✓ renders

Describes the value of an Apple Pencil double-tap gesture.

How it works

PencilDoubleTapGestureValue is the value SwiftUI delivers to your app each time the person performs the system double-tap gesture on a supported Apple Pencil. It carries the contextual information about that interaction — most notably the hover location where the gesture occurred — so your code can respond to the squeeze without managing the low-level Pencil hardware events yourself. Reach for it inside the handler of an Apple Pencil double-tap gesture when you want to switch tools, toggle a mode, or otherwise react to the person tapping their Pencil.

  1. Receive the value from .onPencilDoubleTap

    PencilDoubleTapGestureValue is never constructed by you; SwiftUI creates one and hands it to the closure you register. Here the gesture is attached with .onPencilDoubleTap { (value: PencilDoubleTapGestureValue) in ... }, so value is the live record of the double-tap that just happened on the modified view.

  2. Type the closure parameter explicitly

    Annotating the closure parameter as (value: PencilDoubleTapGestureValue) documents the contract and gives you access to the gesture's members. In the example the body simply runs tapCount += 1 on each delivery, but value is what you would inspect for the gesture's details when you need them.

  3. Read the hover location for context

    PencilDoubleTapGestureValue exposes where the interaction took place through its hover location, letting you tailor the response to the area of the canvas the person is working in. The demo ignores position and just counts taps, but the same value is what you would query to make the reaction location-aware.

  4. Drive view state from the gesture

    Because the handler runs on the main actor with your view's state in scope, you mutate published state directly. Here @State private var tapCount is incremented inside the closure, and the bound Text("Double-taps: \(tapCount)") re-renders to reflect each PencilDoubleTapGestureValue SwiftUI delivers.

Try it — Replace tapCount += 1 with tapCount += 1; print(value) so each delivered PencilDoubleTapGestureValue logs its details when you double-tap the Pencil.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

PencilDoubleTapGestureValue.swift
struct PencilDoubleTapGestureValueDemo: View {
    @State private var tapCount = 0

    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "applepencil")
                .font(.largeTitle)
            Text("Double-taps: \(tapCount)")
                .font(.headline)
            Text("Double-tap your Apple Pencil")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding()
        .onPencilDoubleTap { (value: PencilDoubleTapGestureValue) in
            tapCount += 1
        }
    }
}
Live preview
Double-taps: 0 Double-tap your Apple Pencil
Double-taps: 0 Double-tap your Apple Pencil
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →