Sample CodeiOS, iPadOS, Mac CatalystReviewed 2026-07-21View on Apple Developer

Detecting moving objects in a video

At a glance

Item Summary
Purpose Identify the trajectory of a thrown object by using Vision.
App architecture A Swift sample with the source-visible chain AppDelegateCameraViewControllerVision APIs.
Main patterns View-controller organization, Protocol-oriented abstraction, Delegate or data-source callbacks
Project style 11 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: DispatchQueue(label:), DispatchQueue.main.async; none alone proves a background thread.
State/event model No structured observation or publisher-scheduling marker indexed.
Key frameworks/packages UIKit, AVFoundation, SpriteKit, Vision, UniformTypeIdentifiers; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── VisionTrajectoryDemo/
│   ├── AppDelegate.swift
│   ├── CameraViewController.swift
│   ├── Custom Views/
│   │   ├── VideoRenderView.swift
│   │   └── CameraFeedView.swift
│   ├── ContentAnalysisViewController.swift
│   ├── HomeViewController.swift
│   ├── SceneDelegate.swift
│   ├── SpriteKit Views/
│   │   ├── BallScene.swift
│   │   ├── TrajectoryView.swift
│   │   └── Animations.swift
│   └── AppError.swift
└── Configuration/
    └── SampleCode.xcconfig

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 7 project/configuration file(s) and 14 source declaration(s).

Overall architecture

Reference code

VisionTrajectoryDemo/AppDelegate.swift:10 — architecture anchor

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    // ...
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }
    // ...
}

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 Vision.

Ownership and state

Ownership evidence

VisionTrajectoryDemo/Custom Views/VideoRenderView.swift:18 — stored dependency or nearest verified ownership anchor

class VideoRenderView: UIView, NormalizedRectConverting {
    // ...
    private var renderLayer: CALayer!
    // ...
}
Owner Object or state Relationship Mutation authority
VideoRenderView CALayer (renderLayer) stores or receives Owning lexical scope
ContentAnalysisViewController UIButton (closeButton) stores or receives App/module collaborators
ContentAnalysisViewController AVAsset (recordedVideoSource) stores or receives App/module collaborators
ContentAnalysisViewController CameraViewController (cameraViewController) stores or receives Owning lexical scope

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. VisionTrajectoryDemo/CameraViewController.swift:23
Queue scheduling DispatchQueue.main.async The source addresses the main dispatch queue. VisionTrajectoryDemo/CameraViewController.swift:189

@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

VisionTrajectoryDemo/CameraViewController.swift:23 — representative execution boundary

class CameraViewController: UIViewController {
    // ...
    private let videoDataOutputQueue = DispatchQueue(label: "CameraFeedDataOutput",
                                                     qos: .userInitiated,
                                                     attributes: [],
                                                     autoreleaseFrequency: .workItem)
    // ...
}

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
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. VisionTrajectoryDemo/AppDelegate.swift:8
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. VisionTrajectoryDemo/CameraViewController.swift:9
Source import SpriteKit The cited file imports this module; runtime use and architectural role are not inferred. VisionTrajectoryDemo/SpriteKit Views/BallScene.swift:9
Source import Vision The cited file imports this module; runtime use and architectural role are not inferred. VisionTrajectoryDemo/ContentAnalysisViewController.swift:10
Source import UniformTypeIdentifiers The cited file imports this module; runtime use and architectural role are not inferred. VisionTrajectoryDemo/HomeViewController.swift:10

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

VisionTrajectoryDemo/CameraViewController.swift:11 — representative type boundary

protocol CameraViewControllerOutputDelegate: AnyObject {
    func cameraViewController(_ controller: CameraViewController,
                              didReceiveBuffer buffer: CMSampleBuffer,
                              orientation: CGImagePropertyOrientation)
}
Type Responsibility Depends on or conforms to
AppDelegate Receives callback-driven events UIResponder, UIApplicationDelegate
CameraViewControllerOutputDelegate Defines a capability or collaboration contract AnyObject
CameraViewController View lifecycle, callbacks, and feature coordination UIViewController
VideoRenderView User-interface presentation and input forwarding UIView, NormalizedRectConverting
ContentAnalysisViewController View lifecycle, callbacks, and feature coordination UIViewController
CameraFeedView User-interface presentation and input forwarding UIView, NormalizedRectConverting
HomeViewController View lifecycle, callbacks, and feature coordination UIViewController
SceneDelegate Receives callback-driven events UIResponder, UIWindowSceneDelegate
BallScene Scene lifecycle or scene-level composition SKScene
TrajectoryView User-interface presentation and input forwarding SKView, AnimatedTransitioning

The source explicitly defines local protocol relationships: CameraFeedViewNormalizedRectConverting, VideoRenderViewNormalizedRectConverting, TrajectoryViewAnimatedTransitioning, ContentAnalysisViewControllerCameraViewControllerOutputDelegate.

Access control

Symbol Access Verified effect Likely rationale
videoDataOutputQueue (VisionTrajectoryDemo/CameraViewController.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.
cameraFeedView (VisionTrajectoryDemo/CameraViewController.swift:28) 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.
cameraFeedSession (VisionTrajectoryDemo/CameraViewController.swift:29) 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.
videoRenderView (VisionTrajectoryDemo/CameraViewController.swift:30) 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.

Reference code

VisionTrajectoryDemo/CameraViewController.swift:23 — representative boundary

class CameraViewController: UIViewController {
    // ...
    private let videoDataOutputQueue = DispatchQueue(label: "CameraFeedDataOutput",
                                                     qos: .userInitiated,
                                                     attributes: [],
                                                     autoreleaseFrequency: .workItem)
    // ...
}

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 CameraViewController, ContentAnalysisViewController, HomeViewController The source’s Controller suffix makes this role explicit.
Receives callback-driven events AppDelegate, CameraViewControllerOutputDelegate, SceneDelegate The source’s Delegate suffix makes this role explicit.
Scene lifecycle or scene-level composition BallScene The source’s Scene suffix makes this role explicit.
User-interface presentation and input forwarding CameraFeedView, TrajectoryView, VideoRenderView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
View-controller organization VisionTrajectoryDemo/CameraViewController.swift:17 A controller is the verified coordination boundary; this is MVC-style only where a separate model is present.
Protocol-oriented abstraction VisionTrajectoryDemo/Custom Views/CameraFeedView.swift:11 A local protocol and concrete conformance create an explicit capability boundary.
Delegate or data-source callbacks VisionTrajectoryDemo/AppDelegate.swift:11 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: Controller: CameraViewController, ContentAnalysisViewController, HomeViewController; Delegate: AppDelegate, CameraViewControllerOutputDelegate, SceneDelegate; Scene: BallScene; View: CameraFeedView, TrajectoryView, VideoRenderView.
  • Protocols: CameraViewControllerOutputDelegate, NormalizedRectConverting, AnimatedTransitioning.
  • Methods: application, cameraViewController, viewDidDisappear, setupAVSession, viewRectForVisionRect, setupVideoOutputView, startReadingAsset, captureOutput.
  • Files: VisionTrajectoryDemo/AppDelegate.swift, VisionTrajectoryDemo/CameraViewController.swift, VisionTrajectoryDemo/Custom Views/VideoRenderView.swift, VisionTrajectoryDemo/ContentAnalysisViewController.swift, VisionTrajectoryDemo/Custom Views/CameraFeedView.swift, VisionTrajectoryDemo/HomeViewController.swift.

Architecture takeaways

  • AppDelegate is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches UIKit, AVFoundation, SpriteKit, Vision 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
VisionTrajectoryDemo/AppDelegate.swift Cited implementation, UIKit, AppDelegate
VisionTrajectoryDemo/Custom Views/VideoRenderView.swift Cited implementation, NormalizedRectConverting, VideoRenderView
VisionTrajectoryDemo/CameraViewController.swift CameraViewControllerOutputDelegate, Cited implementation, CameraViewController, DispatchQueue(label:), DispatchQueue.main.async, AVFoundation
VisionTrajectoryDemo/Custom Views/CameraFeedView.swift Cited implementation, CameraFeedView
VisionTrajectoryDemo/SpriteKit Views/BallScene.swift SpriteKit, BallScene
VisionTrajectoryDemo/ContentAnalysisViewController.swift Vision, ContentAnalysisViewController
VisionTrajectoryDemo/HomeViewController.swift UniformTypeIdentifiers, HomeViewController
VisionTrajectoryDemo/SceneDelegate.swift SceneDelegate
VisionTrajectoryDemo/SpriteKit Views/TrajectoryView.swift TrajectoryView
VisionTrajectoryDemo/SpriteKit Views/Animations.swift AnimatedTransitionType, AnimatedTransitioning
VisionTrajectoryDemo/AppError.swift AppError