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.
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 ... }, sovalueis the live record of the double-tap that just happened on the modified view.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 runstapCount += 1on each delivery, butvalueis what you would inspect for the gesture's details when you need them.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
valueis what you would query to make the reaction location-aware.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 tapCountis incremented inside the closure, and the boundText("Double-taps: \(tapCount)")re-renders to reflect each PencilDoubleTapGestureValue SwiftUI delivers.
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.
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
}
}
}