Input and Events

Supply actions for a view to perform in response to user input and system events.

Topics

Hiding and Disabling Views

func hidden() -> View

Hides the view.

func disabled(Bool) -> View

Adds a condition for whether users can interact with the view.

Handling View Taps and Gestures

func onTapGesture(count: Int, perform: () -> Void) -> View

Adds an action to perform when this view recognizes a tap gesture.

func gesture<T>(T, including: GestureMask) -> View

Attaches a gesture to the view with a lower precedence than gestures defined by the view.

func highPriorityGesture<T>(T, including: GestureMask) -> View

Attaches a gesture to the view with a higher precedence than gestures defined by the view.

func simultaneousGesture<T>(T, including: GestureMask) -> View

Attaches a gesture to the view to process simultaneously with gestures defined by the view.

func transaction((inout Transaction) -> Void) -> View

Applies the given transaction mutation function to all transactions used within the view.

struct GestureMask

Options that control how adding a gesture to a view affect’s other gestures recognized by the view and its subvies.

Tracking Digital Crown Rotation

Handling View Events

func onAppear(perform: (() -> Void)?) -> View

Adds an action to perform when the view appears.

func onDisappear(perform: (() -> Void)?) -> View

Adds an action to perform when the view disappears.

func onCutCommand(perform: (() -> [NSItemProvider])?) -> View

Adds an action to perform in response to the system’s Cut command.

func onCopyCommand(perform: (() -> [NSItemProvider])?) -> View

Adds an action to perform in response to the system’s Copy command.

func onPasteCommand(of: [String], perform: ([NSItemProvider]) -> Void) -> View

Adds an action to perform in response to the system’s Paste command.

func onPasteCommand<Payload>(of: [String], validator: ([NSItemProvider]) -> Payload?, perform: (Payload) -> Void) -> View

Adds an action to perform in response to the system’s Paste command with items that you validate.

func onDeleteCommand(perform: (() -> Void)?) -> View

Adds an action to perform in response to the system’s Delete command.

func onMoveCommand(perform: ((MoveCommandDirection) -> Void)?) -> View

Adds an action to perform in response to a move command, like when the user presses an arrow key on a Mac keyboard, or taps the edge of the Siri Remote when controlling an Apple TV.

func onExitCommand(perform: (() -> Void)?) -> View

Sets up an action that triggers in response to receiving the exit command while the view has focus.

func onPlayPauseCommand(perform: (() -> Void)?) -> View

Adds an action to perform in response to the system’s Play/Pause command.

func onCommand(Selector, perform: (() -> Void)?) -> View

Adds an action to perform in response to the given selector.

enum MoveCommandDirection

Specifies the direction of an arrow key movement.

func deleteDisabled(Bool) -> View

Adds a condition for whether the view’s view hierarchy is deletable.

func moveDisabled(Bool) -> View

Adds a condition for whether the view's view hierarchy is movable.

Handling Publisher Events

func onReceive<P>(P, perform: (P.Output) -> Void) -> View

Adds an action to perform when this view detects data emitted by the given publisher.

struct SubscriptionView

A view that subscribes to a publisher with an action.

Handling View Hover and Focus

func onHover(perform: (Bool) -> Void) -> View

Adds an action to perform when the user moves the pointer over or away from the view’s frame.

func focusable(Bool, onFocusChange: (Bool) -> Void) -> View

Specifies if the view is focusable and, if so, adds an action to perform when the view comes into focus.

Supporting Drag and Drop in Views

func onDrag(() -> NSItemProvider) -> View

Activates the view as the source of a drag and drop operation.

func onDrop(of: [String], delegate: DropDelegate) -> View

Defines the destination of a drag and drop operation with the same size and position as the view using behavior controlled by the given delegate.

func onDrop(of: [String], isTargeted: Binding<Bool>?, perform: ([NSItemProvider], CGPoint) -> Bool) -> View

Defines the destination of a drag and drop operation with the same size and position as the view by handling dropped content and the drop location with the specified closure.

func onDrop(of: [String], isTargeted: Binding<Bool>?, perform: ([NSItemProvider]) -> Bool) -> View

Defines the destination for a drag and drop operation with the same size and position as the view by handling dropped content with the specified closure.

func itemProvider(Optional<() -> NSItemProvider?>) -> View

Provides a closure that vends the drag representation to be used for a particular data element.

struct DropInfo

The current state of a drop.

struct DropProposal

The behavior of a drop.

enum DropOperation

Operation types that determine how a drag and drop session resolves when the user drops a drag item.

protocol DropDelegate

An interface that you implement to interact with a drop operation in a view modified to accept drops.

Configuring a View for Hit Testing

func allowsHitTesting(Bool) -> View

Configures whether this view participates in hit test operations.

func contentShape<S>(S, eoFill: Bool) -> View

Defines the content shape for hit testing.

See Also

Providing Interactivity

View Presentation

Define additional views for the view to present under specified conditions.

State

Access view preferences and provide child views with configuration data.