Creating 2D shapes with SwiftUI
At a glance
| Item | Summary |
|---|---|
| Purpose | Draw two-dimensional shapes in your visionOS app with SwiftUI shapes or with your custom shapes. |
| App architecture | A code-light SwiftUI sample: EntryPoint declares WindowGroup, MainView owns the visible behavior, and there is no separate service or persistence layer. |
| Main patterns | SwiftUI scene composition |
| Project style | Code-light sample with 5 scanned Swift file(s) and 172 Swift line(s); resources and generated assets are excluded from those counts. |
Project structure
Source bundle/
└── Creating 2D Shapes/
├── EntryPoint.swift # EntryPoint
├── MainView.swift # MainView
├── ShapesView.swift # ShapesView
├── Line.swift # Line
└── Triangle.swift # Triangle
Structure observations
- The runtime boundary is WindowGroup; the pruned tree lists only files that explain lifecycle, state, or framework integration.
- App code is compact; any explicit model, provider, or entity owner is still shown below rather than collapsed into view-local state.
Overall architecture
flowchart LR
EntryPoint_1["EntryPoint"]
WindowGroup_2["WindowGroup"]
MainView_3["MainView"]
SwiftUI_scene_APIs_4["SwiftUI scene APIs"]
EntryPoint_1 --> WindowGroup_2
WindowGroup_2 --> MainView_3
MainView_3 --> SwiftUI_scene_APIs_4
Reference code
Creating 2D Shapes/EntryPoint.swift:11 — the app or executable entry declares the outer scene lifecycle.
struct EntryPoint: App {
var body: some Scene {
WindowGroup {
MainView()
}
}
}The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It keeps scene ownership, shared state, RealityKit content, and framework-provider work at separate levels.
Ownership and state
classDiagram
EntryPoint_1 --> MainView_2 : scene content
MainView_2 --> Framework_3 : invokes framework APIs
Ownership evidence
Creating 2D Shapes/MainView.swift:10 — representative stored state or the nearest verified lifecycle anchor.
struct MainView: View {
var body: some View {
ShapesView()
}
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
EntryPoint / MainView |
Framework-managed scene/view state | No separate long-lived owner is declared | SwiftUI/RealityKit lifecycle closures perform updates |
Ownership here is deliberately narrow: @Environment and weak references are shared links, initialized @State or stored services are lifecycle ownership, and a RealityView content closure owns additions to its entity graph without making the SwiftUI view a reference-type owner.
Class and protocol design
| Type | Responsibility | Depends on or conforms to |
|---|---|---|
EntryPoint (Creating 2D Shapes/EntryPoint.swift:11) |
Declares app scenes and top-level dependency lifetime. | App |
MainView (Creating 2D Shapes/MainView.swift:10) |
Presents UI and forwards gestures or lifecycle events. | View |
ShapesView (Creating 2D Shapes/ShapesView.swift:11) |
Presents UI and forwards gestures or lifecycle events. | View |
The source defines no local substitution protocol in the reviewed boundary. Its protocol use is framework-facing (App, View, RealityKit/ARKit protocols, or platform adapters), so this document does not label the whole app protocol-oriented.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|
No reviewed declaration uses fileprivate, private(set), public, open; unmodified Swift declarations are internal.
Reference code
Creating 2D Shapes/EntryPoint.swift:11 — representative visibility boundary.
struct EntryPoint: App {
// ...
}Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Scene declaration and dependency lifetime | EntryPoint |
The App/entry boundary determines window, volume, and immersive-space lifetime. |
| Presentation, attachments, and gestures | MainView |
SwiftUI view code forwards user intent and RealityView lifecycle events. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| SwiftUI scene composition | Creating 2D Shapes/EntryPoint.swift:11 |
Keeps windows, volumes, and immersive-space lifecycle visible at the app boundary. |
Naming conventions
- Role suffixes are evidence, not decoration: View:
MainView,ShapesView. - Protocols: no app-defined protocol in the reviewed source.
- Commands use verb-led methods:
path,dashed. - Files generally match their primary type;
Views,Models,Managers,Providers,Components,Systems, andPackagesfolders describe architectural roles where present.
Architecture takeaways
- Treat
EntryPointas the owner of scene declarations, not as the owner of every RealityKit entity created later. - Keep view-local interaction in SwiftUI, but move provider sessions, playback resources, shared game state, or transport state into a stable owner when their lifetime exceeds one render pass.
- The compact source does not justify adding repository, coordinator, or protocol layers beyond the model, provider, or view boundary the sample actually declares.
Source map
| Source file | Relevant symbols |
|---|---|
Creating 2D Shapes/EntryPoint.swift:11 |
EntryPoint |
Creating 2D Shapes/MainView.swift:10 |
MainView |
Creating 2D Shapes/ShapesView.swift:11 |
ShapesView |
Creating 2D Shapes/Line.swift:11 |
Line |
Creating 2D Shapes/Triangle.swift:11 |
Triangle |