Spatial SwiftUI: onWorldRecenter

We can use this modifier to execute code after the user has re-centered their view.

New in visionOS 26, onWorldRecenter is a SwiftUI view modifier that will perform a block of code when the user re-centers (Press and hold the Digital Crown). We can use it in windows, volumes, and spaces.

var body: some View {
    VStack {
      ...
    }
    .onWorldRecenter {
        bounceTheWorld()
    }
}

Some uses include:

  • Play vision effects
  • Reset or refocus UI elements
  • Update coordinate calculations on views and entities

In this short demo we move away from the windows, then use “Re-Center Open Apps” in the Simulator Device menu.

Example Code

struct Example102: View {

    @State private var scale: CGFloat = 1.0

    fileprivate func bounceTheWorld() {
        withAnimation(.easeOut(duration: 0.2)) { scale = 1.4 }
        withAnimation(.easeIn(duration: 0.3).delay(0.2)) { scale = 1.0 }
    }

    var body: some View {
        VStack {
            ModelViewSimple(name: "Earth", bundle: realityKitContentBundle)
                .frame(width: 200, height: 200)
                .scaleEffect(scale)
        }
        .onWorldRecenter {
            bounceTheWorld()
        }
    }
}

Support our work so we can continue to bring you new examples and articles.

Download the Xcode project with this and many more examples from Step Into Vision.
Some examples are provided as standalone Xcode projects. You can find those here.

Questions or feedback?