forked from heckj/CameraControlARView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARViewContainer.swift
85 lines (74 loc) · 2.74 KB
/
ARViewContainer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// ARViewContainer.swift
//
//
// Created by Joseph Heck on 2/9/22.
//
#if os(iOS)
import UIKit
#elseif os(macOS)
import Cocoa
typealias UIViewRepresentable = NSViewRepresentable
#endif
import RealityKit
import SwiftUI
/// A SwiftUI representable view that wraps an underlying augmented reality view with camera controls instance.
///
/// Create an ``CameraControlARView`` externally and hand it into the container so that you can interact with the
/// view controls, or the underlying scene, from within SwiftUI.
public struct ARViewContainer: UIViewRepresentable {
/// The type of view this container wraps.
public typealias NSViewType = RealityKit.ARView
/// The wrapped ARView with camera controls enabled.
public var arView: CameraControlARView
// @Weak private var weakUpdates: []
/// Creates a coordinator to establish the view and to pass updates to and from the SwiftUI context hosting the view.
public func makeCoordinator() -> ARViewContainer.ARViewCoordinator {
ARViewCoordinator(self)
}
/// Creates a new SwiftUI view.
#if os(iOS)
public func makeUIView(context _: Context) -> ARView {
let arView = arView
return arView
}
#elseif os(macOS)
public func makeNSView(context _: Context) -> ARView {
// Creates the view object and configures its initial state.
//
// Context includes:
// - coordinator
// - transaction
// - environment
let arView = arView
return arView
}
#endif
#if os(iOS)
/// Updates the wrapped AR view with state information from SwiftUI.
public func updateUIView(_: ARView, context _: Context) {
// Updates the state of the specified view with new information from SwiftUI.
}
#elseif os(macOS)
public func updateNSView(_: ARView, context _: Context) {
}
#endif
/// Creates a new SwiftUI view that wraps and displays an augmented reality view.
/// - Parameter cameraARView: An instance of the camera-controlled AR View.
public init(cameraARView: CameraControlARView) {
self.arView = cameraARView
}
/// The coordinator object that facilitates to and from the wrapped view.
public class ARViewCoordinator: NSObject {
/*
When you want your view controller to coordinate with other SwiftUI views,
you must provide a Coordinator object to facilitate those interactions.
For example, you use a coordinator to forward target-action and
delegate messages from your view controller to any SwiftUI views.
*/
public var representableContainer: ARViewContainer
public init(_ representableContainer: ARViewContainer) {
self.representableContainer = representableContainer
}
}
}