如何在一个 iOS 应用中 运行 两种独立的 ARKit 体验

How to run two separate ARKit experiences in one iOS app

我在一个应用中 运行 设置两个 ARKit 体验时遇到问题。我有主菜单视图控制器,其中 运行 有两种体验。它看起来像这样:

两种体验都是独立的,彼此之间不需要传递任何数据。 不幸的是,当我关闭 ARVC1ARVC2 和 运行 第二个时,不知何故我有前一个 ARSession 的痕迹(前一个会话的静止帧偶尔闪烁一次) .我能以某种方式完全重置 ARSession 吗?它似乎停留在后台的某个地方。我试图在关闭任何视图控制器时暂停会话,但没有帮助。

如果我打开 ARVC1 并按下按钮以显示统计信息,也会发生同样的情况:

arView.debugOptions = [.showStatistics]

关闭 ARVC1 并再次打开它后,它仍然显示统计信息,因此它 运行 在后台某处运行。

我找到了问题的解决方案:

在打开和关闭 ARVC1 并快速打开 ARVC2 之后,我遇到了当前 ARSession 之前的 ARSession 闪烁。为了解决这个问题,我在我的 MainVC 中创建了 ARView 并将对它的引用传递给目标控制器,在那里我以编程方式设置了 arView 约束。为了避免传递 ARAnchors,我使用 viewDidLoad:

中的选项重置了配置
arView.session.run(configuration, options: [.resetTracking,.removeExistingAnchors,.stopTrackedRaycasts])

它也有助于 UINavigationController 过渡(使其更平滑),因为 ARSession 是在显示视图之前配置的,它消除了 [=35] 时出现的黑色闪光=] 新 ARConfigurationARSession.

它还消除了我在快速关闭和打开包含 ARViewViewController 时遇到的控制台错误(下图),该错误有时会使应用程序崩溃:

[Session] Session (0x160879b40): did fail with error: Error Domain=com.apple.arkit.error Code=102 "Required sensor failed." UserInfo={NSLocalizedFailureReason=A sensor failed to deliver the required input., NSUnderlyingError=0x283f959b0 {Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12780), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x283efc8a0 {Error Domain=NSOSStatusErrorDomain Code=-12780 "(null)"}}}, NSLocalizedRecoverySuggestion=Make sure that the application has the required privacy settings., NSLocalizedDescription=Required sensor failed.} 2020-01-09 16:20:38.600835+0000 App[573:231750] [Technique] World tracking performance is being affected by resource constraints [3] 2020-01-09 16:20:38.600977+0000 App[573:231750] [Technique] VIO error callback: 8506.590769, 3, Frame time stamps are either out of order or repeating