Sample CodeiOS, iPadOS, Mac Catalyst, macOS, tvOSReviewed 2026-07-21View on Apple Developer

Supporting Game Controllers

At a glance

Item Summary
Purpose Support a physical controller or add a virtual controller to enhance how people interact with your game through haptics, lighting, and motion sensing.
App architecture A Swift sample with the source-visible chain AppDelegateGameControllerGameController APIs.
Main patterns View-controller organization, Delegate or data-source callbacks
Project style 21 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue(label:), DispatchQueue.main.asyncAfter; none alone proves a background thread.
State/event model Source-visible mechanisms: NotificationCenter.
Key frameworks/packages SceneKit, SpriteKit, GameController, GameplayKit, simd; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── Swift/
    ├── Shared/
    │   ├── GameController.swift
    │   ├── ChaserComponent.swift
    │   ├── HapticUtility.swift
    │   ├── ScaredComponent.swift
    │   ├── BaseComponent.swift
    │   └── PlayerComponent.swift
    ├── iOS/
    │   ├── AppDelegate.swift
    │   └── GameViewController.swift
    ├── macOS/
    │   ├── AppDelegate.swift
    │   └── GameViewController.swift
    └── tvOS/
        ├── AppDelegate.swift
        └── GameViewController.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 10 project/configuration file(s) and 30 source declaration(s).

Overall architecture

Reference code

Swift/iOS/AppDelegate.swift:10 — architecture anchor

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Game Controller.

Ownership and state

Ownership evidence

Swift/Shared/GameController.swift:17 — stored dependency or nearest verified ownership anchor

struct Bitmask: OptionSet {
    let rawValue: Int
    // ...
}
Owner Object or state Relationship Mutation authority
Bitmask Int (rawValue) owns value state Initialized by the owner; the binding is immutable
Bitmask Bitmask (character) creates and retains Initialized by the owner; the binding is immutable
Bitmask Bitmask (collision) creates and retains Initialized by the owner; the binding is immutable
Bitmask Bitmask (enemy) creates and retains Initialized by the owner; the binding is immutable

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Queue scheduling DispatchQueue(label:) The source constructs a dispatch queue; its label alone does not prove a thread. Swift/Shared/Character.swift:332
Queue scheduling DispatchQueue.main.asyncAfter The source addresses the main dispatch queue. Swift/Shared/Character.swift:362

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

Swift/Shared/Character.swift:332 — representative execution boundary

class Character: NSObject {
    // ...
    var lightQueue = DispatchQueue(label: "lavaLightFlash", qos: .default)
    // ...
}

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation NotificationCenter NotificationCenter distributes named process-local events. Swift/Shared/GameController.swift:123
Source import SceneKit The cited file imports this module; runtime use and architectural role are not inferred. Swift/Shared/BaseComponent.swift:9
Source import SpriteKit The cited file imports this module; runtime use and architectural role are not inferred. Swift/Shared/Overlay.swift:10
Source import GameController The cited file imports this module; runtime use and architectural role are not inferred. Swift/Shared/Character.swift:10
Source import GameplayKit The cited file imports this module; runtime use and architectural role are not inferred. Swift/Shared/BaseComponent.swift:8
Source import simd The cited file imports this module; runtime use and architectural role are not inferred. Swift/Shared/BaseComponent.swift:10
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Swift/Shared/Character.swift:8

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

Swift/Shared/UI/Menu.swift:10 — representative type boundary

protocol MenuDelegate: NSObjectProtocol {

    func fStopChanged(_ value: CGFloat)
    func focusDistanceChanged(_ value: CGFloat)
    func debugMenuSelectCameraAtIndex(_ index: Int)
}
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
AppDelegate Receives callback-driven events NSObject, NSApplicationDelegate
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
MenuDelegate Defines a capability or collaboration contract NSObjectProtocol
ButtonOverlayDelegate Defines a capability or collaboration contract NSObjectProtocol
PadOverlayDelegate Defines a capability or collaboration contract NSObjectProtocol
GameController View lifecycle, callbacks, and feature coordination NSObject, ExtraProtocols
ChaserComponent Stores entity-component data or behavior BaseComponent
ScaredComponent Stores entity-component data or behavior BaseComponent
BaseComponent Stores entity-component data or behavior GKComponent

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
EnemyAltitude (Swift/Shared/BaseComponent.swift:14) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
agent (Swift/Shared/BaseComponent.swift:16) private(set) Read access follows the declaration; writes remain in the private scope. Inference: allow observation while reserving invariant-changing writes for the owner.
isAutoMoveNode (Swift/Shared/BaseComponent.swift:17) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
performEnemyDieWithExplosion (Swift/Shared/BaseComponent.swift:66) internal No explicit modifier means the Swift declaration is internal to the module. Inference: the language’s file or module boundary is sufficient for this sample collaboration.

Reference code

Swift/Shared/BaseComponent.swift:14 — representative boundary

class BaseComponent: GKComponent {
    // ...
    public static let EnemyAltitude: Float = -0.46
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Stores entity-component data or behavior BaseComponent, ChaserComponent, PlayerComponent, ScaredComponent The source’s Component suffix makes this role explicit.
View lifecycle, callbacks, and feature coordination GameController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AppDelegate, ButtonOverlayDelegate, MenuDelegate, PadOverlayDelegate The source’s Delegate suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization Swift/Shared/GameController.swift:48 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Delegate or data-source callbacks Swift/iOS/AppDelegate.swift:11 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: Component: BaseComponent, ChaserComponent, PlayerComponent, ScaredComponent; Controller: GameController; Delegate: AppDelegate, ButtonOverlayDelegate, MenuDelegate, PadOverlayDelegate.
  • Protocols: MenuDelegate, ButtonOverlayDelegate, PadOverlayDelegate.
  • Methods: setupGameController, setupCharacter, setupPhysics, setupCollisions, setupFollowCamera, setupAxisAlignedCamera, setupCameraNode, setupCamera.
  • Files: Swift/Shared/GameController.swift, Swift/iOS/AppDelegate.swift, Swift/macOS/AppDelegate.swift, Swift/tvOS/AppDelegate.swift, Swift/Shared/ChaserComponent.swift, Swift/Shared/HapticUtility.swift.

Architecture takeaways

  • AppDelegate is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SceneKit, SpriteKit, GameController, GameplayKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
Swift/iOS/AppDelegate.swift Cited implementation, AppDelegate
Swift/Shared/GameController.swift Cited implementation, GameController, NotificationCenter, Bitmask, ParticleKind, AudioSourceKind
Swift/Shared/UI/Menu.swift MenuDelegate, Menu
Swift/Shared/BaseComponent.swift Cited implementation, SceneKit, GameplayKit, simd, BaseComponent
Swift/Shared/Character.swift DispatchQueue(label:), DispatchQueue.main.asyncAfter, GameController, Foundation, Character, GroundType, func
Swift/Shared/Overlay.swift SpriteKit, Overlay
Swift/macOS/AppDelegate.swift AppDelegate
Swift/tvOS/AppDelegate.swift AppDelegate
Swift/Shared/ChaserComponent.swift ChaserState, ChaserComponent
Swift/Shared/HapticUtility.swift HapticUtility, func
Swift/Shared/ScaredComponent.swift ScaredState, ScaredComponent
Swift/macOS/GameViewController.swift GameViewControllerMacOS, GameViewMacOS
Swift/Shared/PlayerComponent.swift PlayerComponent
Swift/iOS/GameViewController.swift GameViewControllerIOS
Swift/tvOS/GameViewController.swift GameViewControllerTVOS
Swift/iOS/ButtonOverlay.swift ButtonOverlayDelegate, ButtonOverlay