A type-erased scroll target behavior.
Provide this to the ``View/scrollTargetBehavior(_:)`` modifier. When the
underlying behavior changes, the scroll view to which this behavior applies
will be updated.
Use this to dynamically control the scroll target behavior at runtime.
For example, you could provide a paging behavior in compact size classes
and a view aligned behavior otherwise.
@Environment(\.horizontalSizeClass) var sizeClass
var body: some View {
ScrollView { ... }
.scrollTargetBehavior(scrollTargetBehavior)
}
var scrollTargetBehavior: some ScrollTargetBehavior {
sizeClass == .compact
? AnyScrollTargetBehavior(.paging)
: AnyScrollTargetBehavior(.viewAligned)
}
Updates the proposed target that a scrollable view should scroll to.
The system calls this method in two main cases:
- When a scroll gesture ends, it calculates where it would naturally
scroll to using its deceleration rate. The system
provides this calculated value as the target of this method.
- When a scrollable view's size changes, it calculates where it should
be scrolled given the new size and provides this calculates value
as the target of this method.
You can implement this method to override the calculated target
which will have the scrollable view scroll to a different position
than it would otherwise.
Omit the output parameters from the call. The result contains $ret and $out, with target inside $out.