如何用按钮打开/关闭ARKit?

时间:2018-04-01 13:26:57

标签: swift augmented-reality arkit

我正在创建一个简单的功能,可以在按下时打开/关闭ARkit

func arSwitch() {
    case btnPressed arkit = false
    case btnpressed arkit = true 
}

OR

@IBAction func arBtnpressed(_ sender: Any) {

    if arSwitch.isOn {
         sceneView.scene  = scene
    } else {
         sceneView.scene.stop()// Somehthing like this..
    }  
}

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

请尝试以下代码段,

@IBAction func arBtnpressed(_ sender:Any){

if arSwitch.isOn {
     let configuration = ARWorldTrackingConfiguration()
     sceneView.session.run(configuration)

} else {
     sceneView.session = nil
}

}

答案 1 :(得分:0)

尝试以下代码:

import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var label: UILabel!
    var count: Int = 1
    let configuration = ARWorldTrackingConfiguration()

    override func viewDidLoad() {
        super.viewDidLoad()
        sceneView.delegate = self      
        label.text = "On"
        let scene = SCNScene(named: "art.scnassets/model.scn")!
        sceneView.scene = scene
    } 
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        sceneView.session.run(configuration)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        sceneView.session.pause()
    }
    @IBAction func arButtonPressed(_ sender: Any) {
        count += 1

        if count % 2 == 0 {
            sceneView.session.pause()
            label.text = "OFF"
        } else if count % 2 == 1 {
            sceneView.session.run(configuration)
            label.text = "On"
        }
    }
}

希望这会有所帮助。