TechnologiesSwiftUI

SquareAzimuth enum

iOSmacOStvOSwatchOSvisionOS✓ renders

A type describing what direction something is being viewed from along

How it works

SquareAzimuth describes which of the four cardinal directions something is being viewed from along the horizontal plane, snapping a continuous viewing angle down to one of four discrete orientations. Reach for it on visionOS when you need to know roughly where a viewer is standing relative to your content — front, back, left, or right — without tracking the exact azimuth. Because it collapses a 360° range into four @frozen cases, it's well suited to driving coarse, side-aware layout or presentation decisions rather than fine-grained rotation. The example holds one such value in azimuth and uses it to orient an indicator on screen.

  1. Hold an orientation in one of the four cases

    SquareAzimuth is a frozen enum whose cases — back, front, left, and right — each correspond to a horizontal angle of 180°, 0°, 270°, and 90° respectively. You store or pass one the way the example declares let azimuth: SquareAzimuth, giving downstream views a single, exhaustive value to switch on.

  2. Snap a continuous angle with init(closestToAzimuth:)

    Rather than assigning a case by hand, init(closestToAzimuth:) takes an Angle and returns the case whose orientation is closest to it — e.g. an input azimuth ≥ 45° and < 135° resolves to right. This is the bridge from a measured, continuous viewing direction into the four-way SquareAzimuth bucket the example then renders.

  3. Read the snapped rotation from orientation

    The orientation property exposes the case as a Rotation3D snapped to exactly , 90°, 180°, or 270°. That gives you a ready-made 3D rotation to feed your content; in the 2D illustration the same idea is shown by mapping the value onto a rotationEffect(.degrees(...)) so the Image points the matching way.

  4. Combine directions with SquareAzimuth.Set

    When you need more than one direction at once, the nested SquareAzimuth.Set is an OptionSet with members .front, .back, .left, .right, and .all. Build it from a single case via Set(_:) or from a raw bitmask, and use it to express which sides a behavior should apply to.

  5. Compare and hash cases

    Conformance to Equatable and Hashable means you can match cases with ==, switch over them, or use them as dictionary keys and set members. The example leans on this indirectly through String(describing: azimuth), which prints the active case name so you can confirm which direction is in play.

Try it — Change let azimuth: SquareAzimuth = .east to .right and watch both the rotationEffect rotation and the Text label update to reflect the new snapped direction.

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.

SquareAzimuth.swift
struct SquareAzimuthDemo: View {
    let azimuth: SquareAzimuth = .east

    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "location.north.line.fill")
                .font(.largeTitle)
                .rotationEffect(.degrees(Double(azimuth.rawValue) * 90))
            Text("Azimuth: \(String(describing: azimuth))")
                .font(.headline)
        }
        .padding()
    }
}
Live preview
Azimuth: {String(describing: azimuth)}
Azimuth: {String(describing: azimuth)}
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →