如何在单视图应用程序中添加ARKit

时间:2017-07-27 05:49:00

标签: ios ios11 swift4 arkit

我正在使用 Xcode 9 Beta和Swift 4

创建单一视图应用程序。如何在单一应用程序视图中使用参数现实功能? 这是我的代码。这没有任何错误。但我无法在我的场景中得到任何结果。

import UIKit
import ARKit
import SceneKit

class ViewController: UIViewController {

@IBOutlet weak var scaneView: ARSCNView!
override func viewDidLoad() {
    super.viewDidLoad()

    let scene = SCNScene(named: "art.scnassets/ship.scn")!

    scaneView.scene = scene
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let configuration = ARWorldTrackingSessionConfiguration()

    scaneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    scaneView.session.pause()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

1 个答案:

答案 0 :(得分:4)

您缺少委托方法 ARSCNViewDelegate 我认为此代码正常工作并在场景视图中显示结果

 import UIKit
 import ARKit
 import SceneKit

class ViewController: UIViewController, ARSCNViewDelegate {

@IBOutlet weak var sceneView: ARSCNView!

override func viewDidLoad() {
    super.viewDidLoad()

    sceneView.delegate = self

    sceneView.showsStatistics = true

    let scene = SCNScene(named: "art.scnassets/ship.scn")!

    sceneView.scene = scene
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let configuration = ARWorldTrackingSessionConfiguration()

    sceneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    sceneView.session.pause()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}