onScrollTargetVisibilityChange
Adds an action to be called with information about what views would be considered visible.
View ModifieriOS 18.0+macOS 15.0+tvOS 18.0+watchOS 11.0+visionOS 2.0+onScrollTargetVisibilityChange(idType:threshold:_:) in SwiftUI
Declaration
compose().onScrollTargetVisibilityChange<ID>({
threshold?: number,
action: (arg0: ID[]) => void,
})nonisolated func onScrollTargetVisibilityChange<ID>(
idType: ID.Type,
threshold: Double = 0.5,
_ action: @escaping ([ID]) -> Void
) -> some View
where ID : HashableParameters
options.thresholdnumberThe amount required to be visible within the viewport of the the scrollview before the
actionis fired. By default when the view has crossed more than 50% on-screen, the action will be called.options.action*(arg0: ID[]) => voidThe action which will be called when the views within the scroll view cross the provided threshold. The callback will include which views are considered visible.
Discussion
Use this modifier along with the modifier
View.scrollTargetLayout(isEnabled:)
to be informed when the views in the targetted scroll view have crossed
the provided threshold to be considered on/off screen.
ScrollView {
LazyVStack {
ForEach(models) { model in
CardView(model: model)
}
}
.scrollTargetLayout()
}
.onScrollTargetVisibilityChange(for: Model.ID.self, threshold: 0.2) { onScreenCards in
// Disable video playback for cards that are offscreen...
}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.