How it works
SurfaceSnappingInfo is a lightweight value type that captures the state of how a view aligns, or snaps, to a surface as it is positioned. SwiftUI hands you this information when content settles against a target so that you can react to where and how the snap occurred rather than computing those geometric relationships yourself. Reach for it when you need to read the result of a snapping interaction — for example, to update accompanying UI or drive feedback once a draggable element has come to rest against its surface.
Create the value with SurfaceSnappingInfo()
The type is a simple struct you can instantiate directly to obtain a default, empty snapping state. In the example,
SurfaceSnappingInfo()produces the initial value that the view starts from before any snapping has taken place, giving you a concrete instance to observe and pass around.Hold it in observable state
Because SurfaceSnappingInfo is a value type, it composes naturally with SwiftUI's state machinery. Storing it in
@State private var infolets the view own the current snapping state, and any change toinfotriggers a re-render so the displayed information stays in sync with the latest snap.Read the snapping state in your layout
The instance is meant to be inspected, not just stored. Here
String(describing: info)renders the value into the captionText("Info: \(String(describing: info))"), demonstrating how you surface the contents of a SurfaceSnappingInfo — in real use you would branch on its members to decide layout, styling, or feedback once content has snapped.Bind it to the element that snaps
SurfaceSnappingInfo describes the relationship between a view and the surface it settles against, so it belongs alongside the snapping element. The
RoundedRectanglelabeled "Drag to snap" stands in for that draggable content, and theinfovalue is the place you would record the outcome of its interaction with the surface.
SurfaceSnappingInfo() initializer in the @State private var info declaration with a configured instance and watch the String(describing: info) caption update to reflect the new snapping state.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct SurfaceSnappingInfoDemo: View {
@State private var info = SurfaceSnappingInfo()
var body: some View {
VStack(spacing: 12) {
Text("Surface Snapping")
.font(.headline)
RoundedRectangle(cornerRadius: 12)
.fill(.blue.opacity(0.2))
.frame(width: 200, height: 120)
.overlay(Text("Drag to snap"))
Text("Info: \(String(describing: info))")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding()
}
}