<!--
{
  "availability" : [
    "iOS: 17.5.0 -",
    "iPadOS: 17.5.0 -",
    "macCatalyst: 17.5.0 -",
    "macOS: 14.5.0 -",
    "visionOS: 26.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onPencilDoubleTap(perform:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE17onPencilDoubleTap7performQryAA0efG12GestureValueVc_tF"
  },
  "title" : "onPencilDoubleTap(perform:)"
}
-->

# onPencilDoubleTap(perform:)

Adds an action to perform after the user double-taps their Apple Pencil.

```
nonisolated func onPencilDoubleTap(perform action: @escaping (PencilDoubleTapGestureValue) -> Void) -> some View
```

## Parameters

`action`

The action to perform after the user double-taps their
Apple Pencil.

## Return Value

A view that performs `action` after the user double-taps
their Apple Pencil.

## Discussion

You should respect people’s setting for the double-tap gesture by
reading the [`preferredPencilDoubleTapAction`](/documentation/SwiftUI/EnvironmentValues/preferredPencilDoubleTapAction)
environment value, if the setting makes sense in your app. If it
doesn’t, consider giving people a way to specify custom behavior in your
app instead.

In the example below, double-tapping an Apple Pencil will…

- do nothing if this is what the user selected in the Settings app,
- switch the tool to ”Lasso” if this is the action they have configured
  in the app,
- switch the tool to ”Eraser” if they haven’t configured a custom action
  in the app and this is what they selected in the Settings app,
- switch the tool to the last used one otherwise.

```
enum MyDrawingTool: Equatable {
    case brush
    case lasso
    case eraser
    ...
}

enum MyPencilAction: String {
    case switchLasso
    ...
}

@State private var currentTool = MyDrawingTool.brush
@State private var lastTool: MyDrawingTool?

@Environment(\.preferredPencilDoubleTapAction) private var globalAction
@AppStorage("customPencilDoubleTapAction") private var customAction: MyPencilAction?

var body: some View {
    MyDrawingCanvas()
        .onPencilDoubleTap { _ in
            guard globalAction != .ignore else {
                // Respect the user’s preference to ignore the double-tap gesture.
                return
            }
            if let customAction {
                // If a custom action is configured, respect it.
                if customAction == .switchLasso, currentTool != .lasso {
                     (currentTool, lastTool) = (.lasso, currentTool)
                }
            } else if globalAction == .switchEraser, currentTool != .eraser {
                // Switch to eraser if the user prefers it otherwise.
                (currentTool, lastTool) = (.eraser, currentTool)
            } else if let lastTool {
                // Switch to the last used tool by default.
                (currentTool, self.lastTool) = (lastTool, currentTool)
            }
        }
}
```

For more information about Apple Pencil double-tap gestures, see
[Human Interface Guidelines](https://developer.apple.com/design/human-interface-guidelines/apple-pencil-and-scribble#Double-tap).

> Note: If multiple views with the `onPencilDoubleTap` view modifier
> are visible, all their action closures will be performed after the
> user double-taps their Apple Pencil.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)