onScrollPhaseChange
Adds an action to perform when the scroll phase of the first scroll view in the hierarchy changes.
Overloads
2 ways to call compose().onScrollPhaseChange(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().onScrollPhaseChange(
action: (oldPhase: SwiftUI.ScrollPhase, newPhase: SwiftUI.ScrollPhase, context: SwiftUI.ScrollPhaseChangeContext) => void
)nonisolated func onScrollPhaseChange(
_ action: @escaping (ScrollPhase, ScrollPhase, ScrollPhaseChangeContext) -> Void
) -> some ViewParameters
action*(oldPhase: SwiftUI.ScrollPhase, newPhase: SwiftUI.ScrollPhase, context: SwiftUI.ScrollPhaseChangeContext) => voidA closure to run when the scroll phase changes. - term oldPhase: The old scroll phase. - term newPhase: The new scroll phase.
Discussion
Use this modifier to be informed of changes to a scroll view's
phase. A scroll view may be in a variety of different phases like
interacting or decelerating. See ScrollPhase for more information
on the phases of a scroll view.
When the phase of a scroll view changes, the system invokes the action
you provide. In the following example, a selection binding is updated
when the scroll view transitions to the ScrollPhase.decelerating
or ScrollPhase.idle phase.
@Binding var selection: SelectionValue?
ScrollView {
// ...
}
.onScrollPhaseChange { _, newPhase in
if newPhase == .decelerating || newPhase == .idle {
selection = updateSelection()
}
}The system can also provide you with a context containing the geometry of the scroll view at the time of the phase change. You can use the geometry to understand where the scroll view has come or gone between the phase changes and update dependent state on that information. In the following example, whether toolbar content is hidden is determined based on the direction of the last user initiated scroll.
@Binding var hidesToolbarContent: Bool
@State private var lastOffset: CGFloat = 0.0
ScrollView {
// ...
}
.onScrollPhaseChange { oldPhase, newPhase, context in
if newPhase == .interacting {
lastOffset = context.geometry.contentOffset.y
}
if oldPhase == .interacting, newPhase != .animating,
context.geometry.contentOffset.y - lastOffset < 0.0
{
hidesToolbarContent = true
} else {
hidesToolbarContent = false
}
}If multiple scroll views are found within the view hierarchy, only the first one will invoke the closure you provide and a runtime issue will be logged. For example, in the following view, only the vertical scroll view will have its phase changes invoke the provided closure.
VStack {
ScrollView(.vertical) { ... }
ScrollView(.horizontal) { ... }
}
.onScrollPhaseChange { ... }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.