iPad Swift 游乐场不会 运行 ARKit code/crashes

iPad Swift Playgrounds won’t run ARKit code/crashes

我一直在 Swift Playgrounds 上试验 ARKit。我已经编写了起始代码,但是当我 运行 时什么也没有发生。它不评估代码,而是显示显示代码中任何问题的弹出窗口。

我知道我正在使用的代码有效,因为我在 iPad 运行ning 旧版本的 Swift Playgrounds 上使用了相同的代码并且代码有效完美。这似乎是 Swift Playgrounds 3 或 Swift 5 的问题。

这是有趣的部分。当我删除 运行 初始化程序的代码行,以及使视图控制器成为会话和场景委托的代码时,代码 运行 就可以了。当我把它放回去时,它又犯了同样的错误。不知道怎么回事

我正在 运行宁 Swift Playgrounds 3.0 和 iPad 第 6 代。游乐场使用 ARKit、UIKit、SceneKit 和 PlaygroundSupport。

最后,这是一些代码。

// Code inside modules can be shared between pages and other source files.
import ARKit
import SceneKit
import UIKit
extension ARSCNView {
    public func setup(){
        antialiasingMode = .multisampling4X
        automaticallyUpdatesLighting = false
        preferredFramesPerSecond = 60
        contentScaleFactor = 1.0
        if let camera = pointOfView?.camera {
            camera.wantsHDR = true
            camera.wantsExposureAdaptation = true
            camera.exposureOffset = -1
            camera.minimumExposure = -1
            camera.maximumExposure = 3
        }
    }
}
public class vc : UIViewController, ARSessionDelegate, ARSCNViewDelegate {
    var arscn : ARSCNView!
    var scene : SCNScene!
    public override func loadView() {
        arscn = ARSCNView(frame: CGRect(x: 0, y: 0, width: 768, height: 1024))
        arscn.delegate = self
        arscn.setup()
        scene = SCNScene()
        arscn.scene = scene
        var config = ARWorldTrackingConfiguration()
        config.planeDetection = .horizontal
        arscn.session.delegate = self
        self.view = arscn
        arscn.session.run(configåå)
    }
    public func session(_ session: ARSession, didFailWithError error: Error) {
        // Present an error message to the user
    }

    public func sessionWasInterrupted(_ session: ARSession) {
        // Inform the user that the session has been interrupted, for example, by presenting an overlay
    }

    public func sessionInterruptionEnded(_ session: ARSession) {
        // Reset tracking and/or remove existing anchors if consistent tracking is required
    }
}

最后,请注意,我在 playground 主页面中展示实时视图,并将 class 放在共享代码中。

类'的名字使用UpperCamelCasing在底部添加两串代码

此代码适用于macOS Xcode PlaygroundiPad Swift Playgrounds:

import ARKit
import PlaygroundSupport

class LiveVC: UIViewController, ARSessionDelegate, ARSCNViewDelegate {

    let scene = SCNScene()
    var arscn = ARSCNView(frame: CGRect(x: 0,
                                        y: 0,
                                    width: 640,
                                   height: 360))

    override func viewDidLoad() {

        super.viewDidLoad()
        arscn.delegate = self
        arscn.session.delegate = self
        arscn.scene = scene

        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal]
        arscn.session.run(config)
    }

    func session(_ session: ARSession, didFailWithError error: Error) {}
    func sessionWasInterrupted(_ session: ARSession) {}
    func sessionInterruptionEnded(_ session: ARSession) {}
}

PlaygroundPage.current.liveView = LiveVC().arscn
PlaygroundPage.current.needsIndefiniteExecution = true

P.S. Playground on macOS 提示(虽然在使用 ARKit 模块时意义不大):

To turn on Live View in Xcode Playground 11.0 and higher use the following shortcut:

命令+选项+Return

我想出了一个方法来完成这项工作。我所要做的就是将视图控制器分配给一个变量,然后呈现该变量。我不太确定为什么会这样,我只知道它确实有效。

import ARKit
import SceneKit
import UIKit
import PlaygroundSupport

public class LiveVC: UIViewController, ARSessionDelegate, ARSCNViewDelegate {

    let scene = SCNScene()
    public var arscn = ARSCNView(frame: CGRect(x: 0,y: 0,width: 640,height: 360))

    override public func viewDidLoad() {
        super.viewDidLoad()
        arscn.delegate = self
        arscn.session.delegate = self
        arscn.scene = scene
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal]
        arscn.session.run(config)
        view.addSubview(arscn)
    }
    public func session(_ session: ARSession, didFailWithError error: Error) {}
    public func sessionWasInterrupted(_ session: ARSession) {}
    public func sessionInterruptionEnded(_ session: ARSession) {}
}
var vc = LiveVC()
PlaygroundPage.current.liveView = vc
PlaygroundPage.current.needsIndefiniteExecution = true