TechnologiesSwiftUI

SpatialEventCollection struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A collection of spatial input events that target a specific view.

How it works

A SpatialEventCollection is the set of active spatial touch events that a SpatialEventGesture reports as a single, indexable group. Each member describes one in-flight input — its identity, phase, and location — so you can react to several concurrent touches at once rather than tracking them individually. Reach for it whenever a gesture handler needs to know not just that input occurred, but how many distinct events are happening and where, such as for multi-finger or multi-pointer interactions on touch and spatial surfaces.

  1. Receive the collection from SpatialEventGesture

    A SpatialEventCollection is never constructed directly; it arrives as the value passed to the closures of a SpatialEventGesture. In the example, SpatialEventGesture() is attached with .gesture(...), and its update closures hand you the live collection to inspect.

  2. Read activity through onChanged

    The .onChanged closure fires every time the set of active events changes — a touch begins, moves, or lifts. Here the closure binds the incoming SpatialEventCollection as collection, giving you the current snapshot of all events the moment anything shifts.

  3. Count the active events with count

    Because SpatialEventCollection behaves like a collection, members such as count tell you how many spatial events are presently active. The example stores collection.count into count so the Text can display "Active events: \(count)" as fingers or pointers come and go.

  4. Detect the end of interaction with onEnded

    When all events finish, .onEnded runs. The collection is no longer needed there — the example ignores it with _ and resets count = 0, marking that no spatial events remain on the RoundedRectangle.

  5. Iterate events for per-touch detail

    Beyond count, SpatialEventCollection conforms to Collection, so you can loop over it to read each event's identity, phase, and location. The example only uses the count, but the same collection value lets you walk every active event when you need to position UI under each touch.

Try it — Inside .onChanged, replace count = collection.count with a loop over collection that prints each event's location, then place two or three fingers on the "Touch me" card to watch every active event reported at once.

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.

SpatialEventCollection.swift
struct SpatialEventCollectionDemo: View {
    @State private var count = 0

    var body: some View {
        VStack(spacing: 12) {
            Text("Active events: \(count)")
                .font(.headline)
            RoundedRectangle(cornerRadius: 16)
                .fill(.blue.opacity(0.2))
                .frame(width: 160, height: 160)
                .overlay(Text("Touch me"))
                .gesture(
                    SpatialEventGesture()
                        .onChanged { collection in
                            count = collection.count
                        }
                        .onEnded { _ in
                            count = 0
                        }
                )
        }
        .padding()
    }
}
Live preview
Active events: 0 Touch me
Active events: 0 Touch me
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →