Declaration
compose().onPencilSqueeze({
perform: (phase: SwiftUI.PencilSqueezeGesturePhase) => void,
})nonisolated func onPencilSqueeze(
perform action: @escaping (PencilSqueezeGesturePhase) -> Void
) -> some ViewParameters
options.perform*(phase: SwiftUI.PencilSqueezeGesturePhase) => void
Return value
A view that performs action when the user squeezes their Apple Pencil.
Discussion
You should respect people’s setting for the squeeze gesture by reading
the EnvironmentValues.preferredPencilSqueezeAction 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, completing an Apple Pencil squeeze gesture 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,
- spill the ink if this is the action they have configured in the app,
- present a custom contextual palette if they haven’t configured a custom action in the app and this is what they selected in the Settings app.
enum MyPencilAction: String {
case spillInk
...
}
@Environment(\.preferredPencilSqueezeAction) private var preferredAction
@AppStorage("customPencilSqueezeAction") private var customAction: MyPencilAction?
@State private var contextualPaletteAnchor: PopoverAttachmentAnchor?
@State private var contextualPalettePresented = false
var body: some View {
MyDrawingCanvas()
.onPencilSqueeze { phase in
guard preferredAction != .ignore else {
// Skip if this is what the user prefers.
return
}
if let customAction {
// If a custom action is configured, respect it.
if customAction == .spillInk {
switch phase {
// Spill the ink while the user is squeezing their Apple Pencil.
}
}
} else if preferredAction == .showContextualPalette, case let .ended(value) = phase {
// Present a custom contextual palette if the user prefers it.
contextualPaletteAnchor = value.hoverPose?.anchor.map { .point($0) }
contextualPalettePresented = true
}
}
.popover(
isPresented: $contextualPalettePresented,
attachmentAnchor: contextualPaletteAnchor ?? .point(.center)
) {
MyContextualPalette()
}are visible, all their action closures will be performed when the user squeezes 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.