Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Responding to gestures on an entity

At a glance

Item Summary
Purpose Respond to gestures performed on RealityKit entities using input target and collision components.
App architecture GestureResponseApp loads content in ContentView; collision/input-target configuration makes entities gesture targets and ActiveComponent records the small piece of entity state changed by the gesture.
Main patterns SwiftUI scene composition, UI–RealityKit bridge, Component-gated gesture state
Project style Code-light sample with 3 scanned Swift file(s) and 114 implementation line(s).

Project structure

Source bundle/
└── GestureResponse/
    ├── ContentView.swift  # ActiveComponent, ContentView
    └── GestureResponseApp.swift  # GestureResponseApp

Structure observations

  • The runtime boundary is WindowGroup; the tree is pruned to lifecycle, state, framework, rendering, and ECS files.
  • The source is Swift; its compact size does not erase any explicit owner shown below.

Overall architecture

Reference code

GestureResponse/GestureResponseApp.swift:10 — executable or app-lifecycle anchor.

struct GestureResponseApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
     }
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It separates lifecycle, presentation, stable state, framework/session work, and the RealityKit entity graph.

Ownership and state

Ownership evidence

GestureResponse/ContentView.swift:18 — representative stored state or nearest verified lifecycle anchor.

struct ContentView: View {
    var cube: ModelEntity
    // ...
}
Owner Object or state Relationship Mutation authority
ContentView ModelEntity as cube stores and coordinates The owning type coordinates writes

Ownership is intentionally narrow: environment, bindings, observed objects, weak links, and delegate callbacks do not prove lifetime ownership. Initialized stored services and wrapper-managed state do; entities added inside a RealityKit content closure belong to that entity graph.

Class and protocol design

Type Responsibility Depends on or conforms to
GestureResponseApp (GestureResponse/GestureResponseApp.swift:10) Declares app lifecycle and top-level dependency lifetime. App
ContentView (GestureResponse/ContentView.swift:17) Presents UI and forwards gestures or lifecycle events. View
ActiveComponent (GestureResponse/ContentView.swift:13) Stores RealityKit entity data. Component

The reviewed source defines no local substitution protocol. Its App, View, delegate, renderer, ARKit, and RealityKit conformances are framework-facing contracts, so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
public var active: Bool = false (GestureResponse/ContentView.swift:14) public The symbol is available to importing modules, subject to its containing type’s visibility. Inference: Expose an intentional package or cross-target surface.

No reviewed Swift access example uses fileprivate, private(set), open; declarations without a modifier are internal.

Reference code

GestureResponse/ContentView.swift:14 — representative visibility boundary.

@Observable
public class ActiveComponent: Component {
    public var active: Bool = false
}

Logic ownership and placement

Logic Owning type or file Placement rationale
Application/command lifecycle GestureResponseApp The executable boundary determines app, controller, scene, or command lifetime.
Presentation and user intent ContentView The view/controller forwards input and owns only presentation-local work.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition GestureResponse/GestureResponseApp.swift:10 Keeps windows, volumes, and immersive presentation visible at the app boundary.
UI–RealityKit bridge GestureResponse/ContentView.swift:34 Builds or hosts the RealityKit entity graph from SwiftUI or a platform view/controller lifecycle.
Component-gated gesture state GestureResponse/ContentView.swift:13 Uses RealityKit targeting components to admit gestures and stores the resulting active state on the entity.

Naming conventions

  • Role suffixes are evidence, not decoration: App: GestureResponseApp; Component: ActiveComponent; View: ContentView.
  • ECS names pair data and behavior: components ActiveComponent; systems none.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: the compact sample has no separate command layer.
  • Files and folders use primary-type or responsibility names such as Views, Models, Rendering, Components, Systems, Packages, or feature names where those boundaries exist.

Architecture takeaways

  • Treat GestureResponseApp as the owner of executable or scene lifecycle, not automatically as the owner of every entity created later.
  • Keep view/controller input near presentation, but move sessions, reconstruction, capture, rendering, shared game state, or documents into stable owners when their lifetime exceeds one callback or render pass.
  • The compact source does not justify repository, coordinator, or protocol layers beyond boundaries the sample actually declares.

Source map

Source file Relevant symbols
GestureResponse/ContentView.swift:13 ActiveComponent, ContentView
GestureResponse/GestureResponseApp.swift:10 GestureResponseApp