onContinuousHover(coordinateSpace:perform:)
Adds an action to perform when the pointer enters, moves within, and exits the view’s bounds.
Declaration
nonisolated func onContinuousHover(coordinateSpace: some CoordinateSpaceProtocol = .local, perform action: @escaping (HoverPhase) -> Void) -> some View
Parameters
- coordinateSpace:
The coordinate space for the location values. The default value is Local.
- action:
The action to perform whenever the pointer enters, moves within, or exits the view’s bounds. The closure takes a
phaseinput that has the value Active(_:) and contains the pointer’s coordinates if the pointer is within the view’s bounds. The closure receives the Ended phase when the pointer leaves the view’s bounds.
Return Value
A view that calls action when the pointer enters, moves within, or exits the view’s bounds.
Discussion
Use this modifier to define a region for detecting pointer movement with a view. The following example updates a small rectangle’s position and style by modifying hoverLocation and isHovering as the pointer moves within the larger, red rectangle:
@State private var hoverLocation: CGPoint = .zero
@State private var isHovering = false
var body: some View {
Color.red
.frame(width: 400, height: 400)
.onContinuousHover { phase in
switch phase {
case .active(let location):
hoverLocation = location
isHovering = true
case .ended:
isHovering = false
}
}
.overlay {
Rectangle()
.frame(width: 50, height: 50)
.foregroundStyle(isHovering ? .green : .blue)
.offset(x: hoverLocation.x, y: hoverLocation.y)
}
}