以编程方式添加ARSCNView

5
我该如何通过编程方式添加 ARSCNView?我该如何设置宽度、高度和约束?
class ViewController: UIViewController {

    var sceneView: ARSCNView!
    let configuration = ARWorldTrackingConfiguration()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
        self.sceneView.session.run(configuration)
    }
}
2个回答

4

如果您只是想了解如何添加ARSCNView,那么我的答案会是:

//instantiate scene view in viewDidLoad
sceneView = ARSCNView()

//add it to parents subview
self.view.addSubview(sceneView)

//add autolayout contstraints
sceneView.translatesAutoresizingMaskIntoConstraints = false
sceneView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
sceneView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
sceneView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
sceneView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

//load your scene

你能看一下这个问题吗?https://dev59.com/5FYN5IYBdhLWcg3wm5Oi - utiq
我在上面的例子中提到了addSubview的步骤 @utiq - Rikesh Subedi
谢谢!你救了我的一天! - undefined

2
你的代码可能只需要这么简单:

最初的回答可能是这样:

import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    lazy var sceneView: ARSCNView = {
        let sceneView = ARSCNView()
        sceneView.delegate = self
        return sceneView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(sceneView)
    
        NSLayoutConstraint.activate([
            sceneView.topAnchor.constraint(equalTo: view.topAnchor),
            sceneView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            sceneView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            sceneView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
        view.subviews.forEach {
            $0.translatesAutoresizingMaskIntoConstraints = false
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接