以编程方式添加ARSCNView

时间:2017-10-30 03:06:04

标签: ios swift swift3 augmented-reality arkit

如何以编程方式添加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 个答案:

答案 0 :(得分:1)

如果您只是询问如何添加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

答案 1 :(得分:0)

如果您不打算使用约束(例如,您的设备方向为纵向),则ViewController.swift文件中的代码可能就这么简单:

class ViewController: UIViewController, ARSCNViewDelegate {

    let sceneView = ARSCNView(frame: UIScreen.main.bounds) // added 1st line

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(sceneView)                    // added 2nd line

        sceneView.delegate = self
        let scene = SCNScene(named: "art.scnassets/model.scn")!
        sceneView.scene = scene
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let configuration = ARWorldTrackingConfiguration()
        sceneView.session.run(configuration)
    }
}

enter image description here

希望这会有所帮助。