Declaration
compose().onPencilDoubleTap({
perform: (value: SwiftUI.PencilDoubleTapGestureValue) => void,
})nonisolated func onPencilDoubleTap(
perform action: @escaping (PencilDoubleTapGestureValue) -> Void
) -> some ViewParameters
options.perform*(value: SwiftUI.PencilDoubleTapGestureValue) => void
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 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.
are visible, all their action closures will be performed after the user double-taps their Apple Pencil.
See also
More in Events and lifecycle.
- onAppearAdds an action to perform before this view appears.
- onChangeAdds a modifier for this view that fires an action when a specific value changes.
- onContinueUserActivityRegisters a handler to invoke in response to a user activity that your app receives.
- onContinuousHoverAdds an action to perform when the pointer enters, moves within, and exits the view's bounds.
- onDisappearAdds an action to perform after this view disappears.
- onDragActivates this view as the source of a drag and drop operation.
- onDragSessionUpdatedSpecifies an action to perform on each update of an ongoing dragging operation activated by draggable(:) or anther drag modifiers.
- onDropDefines the destination of a drag and drop operation that handles the dropped content with a closure that you specify.
- onDropSessionUpdatedSpecifies an action to perform on each update of an ongoing drop operation activated by dropDestination(:) or other drop modifiers.
- onGeometryChangeAdds an action to be performed when a value, created from a geometry proxy, changes.
- onHingeChangeAdds an action to perform when the hinge context of the view hierarchy changes.
- onHoverAdds an action to perform when the user moves the pointer over or away from the view's frame.