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.

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.

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/Shared/GameController.swift Bitmask, ParticleKind, AudioSourceKind, GameController
Swift/iOS/AppDelegate.swift AppDelegate
Swift/macOS/AppDelegate.swift AppDelegate
Swift/tvOS/AppDelegate.swift AppDelegate
Swift/Shared/ChaserComponent.swift ChaserState, ChaserComponent
Swift/Shared/HapticUtility.swift HapticUtility, func, func, func, func
Swift/Shared/ScaredComponent.swift ScaredState, ScaredComponent
Swift/macOS/GameViewController.swift GameViewControllerMacOS, GameViewMacOS
Swift/Shared/BaseComponent.swift BaseComponent
Swift/Shared/PlayerComponent.swift PlayerComponent