Skip to main content
.coordinateSpace(name: string): Component;

Parameters

name
string
A unique name to identify this coordinate space. This name can be referenced by other views to perform coordinate transformations.

Support

Usage

Named scroll view coordinate space

ScrollView([
    ForEach(items, (item) =>
        Text(item.name)
            .background(
                GeometryReader({ content: (geometry) =>
                    Rectangle()
                        .fill(Color("blue").opacity(
                            geometry.frame("scroll").minY > 0 ? 0.2 : 0
                        ))
                })
            )
    )
]).coordinateSpace("scroll")

Container with named coordinate space

VStack([
    Text("Header"),
    ZStack([
        Rectangle().fill(Color("background")),
        DragGesture()
            .onChanged((value) => {
                const location = value.location
                // location is relative to "container" coordinate space
            })
    ]).coordinateSpace("container")
])

Multiple coordinate spaces

VStack([
    HStack([
        Rectangle()
            .coordinateSpace("leftPanel"),
        Rectangle()
            .coordinateSpace("rightPanel")
    ]).coordinateSpace("mainContainer")
])

Gesture coordinate space reference

Rectangle()
    .fill(Color("red"))
    .onDragGesture((value) => {
        // Drag locations relative to "canvas" coordinate space
        const canvasLocation = value.location
    })
    .coordinateSpace("canvas")

Animation coordinate space

ZStack([
    Rectangle()
        .fill(Color("blue"))
        .position(animationPosition),
    
    Button("Animate", () => {
        withAnimation(() => {
            // Animate to specific position in "animationSpace"
            animationPosition = { x: 100, y: 200 }
        })
    })
]).coordinateSpace("animationSpace")

Nested coordinate spaces

VStack([
    ScrollView([
        VStack([
            Text("Content in nested space")
        ]).coordinateSpace("content")
    ]).coordinateSpace("scroll")
]).coordinateSpace("main")

Interactive drawing area

ZStack([
    Canvas({ content: (context) => {
        // Drawing relative to "drawingArea" coordinate space
        context.stroke(path, Color("black"))
    } }),
    
    DragGesture()
        .onChanged((value) => {
            // Add point relative to "drawingArea"
            addDrawingPoint(value.location)
        })
]).coordinateSpace("drawingArea")

Layout measurement

HStack([
    Text("Measure me")
        .background(
            GeometryReader({ content: (geometry) =>
                Rectangle()
                    .onAppear(() => {
                        const frame = geometry.frame("measurement")
                        recordMeasurement(frame.size)
                    })
            })
        )
]).coordinateSpace("measurement")