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.

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 {
    // ...
}

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.

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

    private let videoDataOutputQueue = DispatchQueue(label: "CameraFeedDataOutput",
                                                     qos: .userInitiated,
                                                     attributes: [],
                                                     autoreleaseFrequency: .workItem)
    
    private var cameraFeedView: CameraFeedView!

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 AppDelegate
VisionTrajectoryDemo/CameraViewController.swift CameraViewControllerOutputDelegate, CameraViewController
VisionTrajectoryDemo/Custom Views/VideoRenderView.swift NormalizedRectConverting, VideoRenderView
VisionTrajectoryDemo/ContentAnalysisViewController.swift ContentAnalysisViewController
VisionTrajectoryDemo/Custom Views/CameraFeedView.swift CameraFeedView
VisionTrajectoryDemo/HomeViewController.swift HomeViewController
VisionTrajectoryDemo/SceneDelegate.swift SceneDelegate
VisionTrajectoryDemo/SpriteKit Views/BallScene.swift BallScene
VisionTrajectoryDemo/SpriteKit Views/TrajectoryView.swift TrajectoryView
VisionTrajectoryDemo/SpriteKit Views/Animations.swift AnimatedTransitionType, AnimatedTransitioning