onGeometryChange
Adds an action to be performed when a value, created from a geometry proxy, changes.
Overloads
2 ways to call compose().onGeometryChange(). Each is one SwiftUI overload, picked by the arguments you pass.
compose().onGeometryChange<T>({
of: (arg0: SymbolicValue) => unknown,
action: (oldValue: T, newValue: T) => void,
})@preconcurrency nonisolated func onGeometryChange<T>(
for type: T.Type,
of transform: @escaping @Sendable (GeometryProxy) -> T,
action: @escaping (T, T) -> Void
) -> some View
where T : Equatable, T : SendableParameters
options.of*(arg0: SymbolicValue) => unknownoptions.action*(oldValue: T, newValue: T) => voidA closure to run when the transformed data changes. - term oldValue: The old value that failed the comparison check. - term newValue: The new value that failed the comparison check.
Discussion
The geometry of a view can change frequently, especially if
the view is contained within a ScrollView and that scroll view
is scrolling.
You should avoid updating large parts of your app whenever the scroll geometry changes. To aid in this, you provide two closures to this modifier:
- transform: This converts a value of
GeometryProxyto your own data type. - action: This provides the data type you created in
ofand is called whenever the data type changes.
For example, you can use this modifier to know how much of a view
is visible on screen. In the following example,
the data type you convert to is a Bool and the action is called
whenever the Bool changes.
ScrollView(.horizontal) {
LazyHStack {
ForEach(videos) { video in
VideoView(video)
}
}
}
struct VideoView: View {
var video: VideoModel
var body: some View {
VideoPlayer(video)
.onGeometryChange(for: Bool.self) { proxy in
let frame = proxy.frame(in: .scrollView)
let bounds = proxy.bounds(of: .scrollView) ?? .zero
let intersection = frame.intersection(
CGRect(origin: .zero, size: bounds.size))
let visibleHeight = intersection.size.height
return (visibleHeight / frame.size.height) > 0.75
} action: { isVisible in
video.updateAutoplayingState(
isVisible: isVisible)
}
}
}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.
- 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.
- onInteractiveResizeChangeAdds an action to perform when the enclosing window is being interactively resized.