Playing Haptics on Game Controllers
At a glance
| Item | Summary |
|---|---|
| Purpose | Add haptic feedback to supported game controllers by using Core Haptics. |
| App architecture | A Swift sample with the source-visible chain AppDelegate → MainViewController → HapticsManager → CoreHaptics APIs. |
| Main patterns | View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks |
| Project style | 5 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
Project structure
Source bundle/
├── HapticControllers/
│ ├── App/
│ │ ├── AppDelegate.swift
│ │ └── SceneDelegate.swift
│ ├── HapticsManager.swift
│ ├── User Interface/
│ │ ├── MainViewController.swift
│ │ └── Base.lproj/
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── Extensions/
│ │ └── UIViewExtensions.swift
│ └── Info.plist
├── Configuration/
│ └── SampleCode.xcconfig
└── HapticControllers.xcodeproj/
├── .xcodesamplecode.plist
└── project.pbxproj
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 6 project/configuration file(s) and 5 source declaration(s).
Overall architecture
flowchart LR
N1["AppDelegate"]
N2["MainViewController"]
N3["HapticsManager"]
N4["CoreHaptics APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
HapticControllers/App/AppDelegate.swift:10 — architecture anchor
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}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 Core Haptics.
Ownership and state
classDiagram
HapticsManager o-- GCController : controller
SceneDelegate o-- UIWindow : window
MainViewController o-- HapticsManager : manager
MainViewController o-- UIButton : highlightedButton
Ownership evidence
HapticControllers/HapticsManager.swift:21 — stored dependency or nearest verified ownership anchor
class HapticsManager {
// ...
private var controller: GCController?
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
HapticsManager |
GCController (controller) |
stores or receives | Owning lexical scope |
SceneDelegate |
UIWindow (window) |
stores or receives | App/module collaborators |
MainViewController |
HapticsManager (manager) |
stores or receives | Initialized by the owner; the binding is immutable |
MainViewController |
UIButton (highlightedButton) |
stores or receives | App/module collaborators |
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
HapticControllers/HapticsManager.swift:12 — representative type boundary
protocol HapticsManagerDelegate: AnyObject {
func didConnect(controller: GCController)
func didDisconnectController()
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
AppDelegate |
Receives callback-driven events | UIResponder, UIApplicationDelegate |
HapticsManagerDelegate |
Defines a capability or collaboration contract | AnyObject |
HapticsManager |
Long-lived feature or framework coordination | Concrete collaborators/imported frameworks |
SceneDelegate |
Receives callback-driven events | UIResponder, UIWindowSceneDelegate |
MainViewController |
View lifecycle, callbacks, and feature coordination | UIViewController |
The source explicitly defines local protocol relationships: MainViewController → HapticsManagerDelegate.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
isSetup (HapticControllers/HapticsManager.swift:19) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
controller (HapticControllers/HapticsManager.swift:21) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
engineMap (HapticControllers/HapticsManager.swift:23) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
startObserving (HapticControllers/HapticsManager.swift:34) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: hide an implementation step that is not part of the collaboration surface. |
Reference code
HapticControllers/HapticsManager.swift:19 — representative boundary
private var isSetup = false
private var controller: GCController?
// A haptic engine manages the connection to the haptic server.
private var engineMap = [GCHapticsLocality: CHHapticEngine]()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 |
|---|---|---|
| View lifecycle, callbacks, and feature coordination | MainViewController |
The source’s Controller suffix makes this role explicit. |
| Receives callback-driven events | AppDelegate, HapticsManagerDelegate, SceneDelegate |
The source’s Delegate suffix makes this role explicit. |
| Long-lived feature or framework coordination | HapticsManager |
The source’s Manager suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | HapticControllers/User Interface/MainViewController.swift:12 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Protocol-oriented abstraction | HapticControllers/User Interface/MainViewController.swift:154 |
A local protocol and concrete conformance create an explicit capability boundary. |
| Delegate or data-source callbacks | HapticControllers/App/AppDelegate.swift:11 |
Callback protocols invert event delivery back into the sample’s owner. |
Naming conventions
- Types: Controller: MainViewController; Delegate: AppDelegate, HapticsManagerDelegate, SceneDelegate; Manager: HapticsManager.
- Protocols:
HapticsManagerDelegate. - Methods:
application,didConnect,didDisconnectController,startObserving,controllerDidConnect,createEngine,controllerDidDisconnect,playHapticsFile. - Files:
HapticControllers/App/AppDelegate.swift,HapticControllers/HapticsManager.swift,HapticControllers/App/SceneDelegate.swift,HapticControllers/User Interface/MainViewController.swift.
Architecture takeaways
AppDelegateis the main source-visible entry or composition anchor for this sample.- Framework work reaches UIKit, CoreHaptics, GameController 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.
- Local protocol relationships provide an explicit substitution boundary.
Source map
| Source file | Relevant symbols |
|---|---|
HapticControllers/App/AppDelegate.swift |
AppDelegate |
HapticControllers/HapticsManager.swift |
HapticsManagerDelegate, HapticsManager |
HapticControllers/App/SceneDelegate.swift |
SceneDelegate |
HapticControllers/User Interface/MainViewController.swift |
MainViewController |
HapticControllers/Extensions/UIViewExtensions.swift |
Feature implementation |