在我的一个iOS App开发项目中,每当我尝试在ARSCNView上推送视图时。应用程序冻结在下面是我的代码。如果您了解相同的解决方案,请建议适当的解决方案。
- >在sceneView
控制器上的推送视图
let session = ARSession()
var sessionConfig: ARSessionConfiguration = ARWorldTrackingSessionConfiguration()
var use3DOFTracking = false {
didSet {
if use3DOFTracking {
sessionConfig = ARSessionConfiguration()
}
sessionConfig.isLightEstimationEnabled = true
session.run(sessionConfig)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupScene()
setupSession()
}
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = false
session.run(sessionConfig, options: [.resetTracking, .removeExistingAnchors])
}
override func viewDidDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
session.pause()
}
func setupScene() {
sceneView.delegate = self
sceneView.session = session
sceneView.antialiasingMode = .multisampling4X
sceneView.automaticallyUpdatesLighting = false
sceneView.preferredFramesPerSecond = 60
sceneView.contentScaleFactor = 1.3
//sceneView.showsStatistics = true
enableEnvironmentMapWithIntensity(25.0)
if let camera = sceneView.pointOfView?.camera {
camera.wantsHDR = true
camera.wantsExposureAdaptation = true
camera.exposureOffset = -1
camera.minimumExposure = -1
}
}
func setupSession() {
if let worldSessionConfig = sessionConfig as? ARWorldTrackingSessionConfiguration {
worldSessionConfig.planeDetection = .horizontal
session.run(worldSessionConfig, options: [.resetTracking, .removeExistingAnchors])
}
}
答案 0 :(得分:2)
您不应因任何原因中断AR会话。尝试使用弹出窗口重构您的设计。
有一个iOS 11 Beta错误,当ARSCNView消失时会冻结ARCamera
,所以,如果你真的需要暂时隐藏AR屏幕:
ARSCNView
,然后从视图控制器中删除IBOutlet
; ARSCNView
时以编程方式创建UIViewController
; ARSCNView
。这是一个简短的例子:
class ViewController: UIViewController, ARSCNViewDelegate {
// Weak reference for scene view.
weak var sceneView: ARSCNView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a new AR Scene View.
let sceneView = ARSCNView(frame: view.bounds)
sceneView.delegate = self
sceneView.showsStatistics = true
view.addSubview(sceneView)
// Create a new scene.
sceneView.scene = SCNScene(named: "art.scnassets/ship.scn")!
// Running session.
sceneView.session.run(ARWorldTrackingSessionConfiguration())
// Saving weak reference.
self.sceneView = sceneView
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Removing current SceneView to avoid freezes.
sceneView?.removeFromSuperview()
}
}
当然,您应该检查框架是否有场景视图,并根据需要设置ARSCNView
,ARSession
和SCNScene
。
Xcode Beta 5
大多数冻结在Beta 5中得到修复。但有时它仍会冻结,就像您使用本机共享视图控制器一样。
答案 1 :(得分:-1)
在我看来,你应该有一个viewDidAppear的覆盖功能,并在那里启动你的setupSession()。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Prevent the screen from being dimmed after a while.
UIApplication.shared.isIdleTimerDisabled = true
// Start the ARSession.
setupSession()
}