Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2023 Spatial ComputingSwiftUI & UI Frameworks

WWDC23 · 20 min · Spatial Computing / SwiftUI & UI Frameworks

Take SwiftUI to the next dimension

Get ready to add depth and dimension to your visionOS apps. Find out how to bring three-dimensional objects to your app using volumes, get to know the Model 3D API, and learn how to position and animate content. We’ll also show you how to use UI attachments in RealityView and support gestures in your content.

Watch at developer.apple.com ↗

Transcript all transcripts

Chapters

Code shown on screen · 2 snippets

MoonView swift · at 3:35 ↗
struct MoonView {
  var body: some View {
    Model3D(named: "Moon") { phase in
      switch phase {
      case .empty:
        ProgressView()
      case let .failure(error):
        Text(error.localizedDescription)
      case let .success(model):
        model
          .resizable()
          .scaledToFit()
      }
    }
  }
}
Manipulation Gesture swift · at 17:26 ↗
// Gesture combining dragging, magnification, and 3D rotation all at once.
var manipulationGesture: some Gesture<AffineTransform3D> {
    DragGesture()
        .simultaneously(with: MagnifyGesture())
        .simultaneously(with: RotateGesture3D())
        .map { gesture in
            let (translation, scale, rotation) = gesture.components()

            return AffineTransform3D(
                scale: scale,
                rotation: rotation,
                translation: translation
            )
        }
}

// Helper for extracting translation, magnification, and rotation.
extension SimultaneousGesture<
    SimultaneousGesture<DragGesture, MagnifyGesture>,
    RotateGesture3D>.Value {
    func components() -> (Vector3D, Size3D, Rotation3D) {
        let translation = self.first?.first?.translation3D ?? .zero
        let magnification = self.first?.second?.magnification ?? 1
        let size = Size3D(width: magnification, height: magnification, depth: magnification)
        let rotation = self.second?.rotation ?? .identity
        return (translation, size, rotation)
    }
}