swift 以编程方式添加ARSCNView

hrysbysz  于 11个月前  发布在  Swift
关注(0)|答案(2)|浏览(110)

如何以编程方式添加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)
    }
}

字符串

z18hc3ub

z18hc3ub1#

如果你只是问如何添加ARSCN视图,那么我的回答是:

//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

字符串

2cmtqfgy

2cmtqfgy2#

你的代码可能就这么简单:

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
        }
    }
}

字符串

相关问题