visionOS: An Introduction

Nov 21 2023 · Swift 5.9, visionOS 1.0 beta, Xcode 15

Lesson 03: Building an Immersive View

Demo Part 1

Episode complete

Play next episode

Next

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Start with the app in the Starter folder, or carry on with the build from lesson 2.

NavigationSplitView {
  Text("Immersive Tab")
    .font(.largeTitle)
    .foregroundColor(.orange)
} detail: {
    Button("Open ImmersiveSpace") {
      //
    }
}
.tabItem {
  Image(systemName: "globe")
  Text("Immersive")
}
RealityView { content in
  if let scene = try? await Entity(named: "ImmersiveScene", in: realityKitContentBundle) {
    content.add(scene)
  }
}
ImmersiveSpace(id: "ImmersiveScene") {
  ImmersiveView()
}
@State private var currentStyle: ImmersionStyle = .full
.immersionStyle(selection: $currentStyle, in: .full)
Button("Open ImmersiveSpace") {

}.foregroundColor(.blue)
Button("Close ImmersiveSpace") {

}.foregroundColor(.red)
@Environment(\.openImmersiveSpace) var openImmersiveScene
@Environment(\.dismissImmersiveSpace) var dismissImmersiveScene
Task {
  let result =  await openImmersiveScene(id: "ImmersiveScene")
  if case .error = result {
    print("An error occurred")
  }
}
Task {
  await dismissImmersiveScene()
  print("Dismissing Complete")
}
.offset(y: -2000)
.offset(z: -1500)
@State private var isShowingImmersive = false
.opacity(isShowingImmersive ? 0 : 1)
Button("Close ImmersiveSpace") {
  Task {
    await dismissImmersiveScene()
    print("Dismissing Complete")
    isShowingImmersive = false
  }
}.foregroundColor(.red)
  .opacity(isShowingImmersive ? 1 : 0)
Button("Open ImmersiveSpace") {
    Task {
      let result =  await openImmersiveScene(id: "ImmersiveScene")
      if case .error = result {
        print("An error occurred")
      }
      isShowingImmersive = true
    }
}.foregroundColor(.blue)
update: { content in
  if let scene = content.entities.first {
    scene.availableAnimations.forEach { animation in
      scene.playAnimation(animation.repeat(), transitionDuration: 3, startsPaused: false)
    }
  }
}
See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Demo Part 2