为什么添加过多的SCNNode时每次都会导致快速操场崩溃?

问题描述 投票:0回答:1

此代码作为常规的iOS应用程序在我的iPhone上运行良好,但是在iPad操场上运行时会崩溃。

code runs on iPhone successfully

该代码只是一个演示,以显示其崩溃。它可以在iPad操场上成功添加一个或两个SCNNode。我的iPad是10.5专业版。

ios swift arkit swift-playground
1个回答
0
投票
import ARKit
import SceneKit
import UIKit
import PlaygroundSupport

public class HomeViewController: UIViewController, ARSessionDelegate, ARSCNViewDelegate {

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

    var point1: SCNSphere!
    var node1: SCNNode!

    var point2: SCNSphere!
    var node2: SCNNode!

    var point3: SCNSphere!
    var node3: SCNNode!

    var point4: SCNSphere!
    var node4: SCNNode!

    var point5: SCNSphere!
    var node5: SCNNode!

    var point6: SCNSphere!
    var node6: SCNNode!

    override public func viewDidLoad() {
        //viewdidload config
        super.viewDidLoad()
        sceneView.delegate = self
        sceneView.session.delegate = self
        sceneView.scene = scene
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal]
        sceneView.session.run(config)
        view.addSubview(sceneView)


        point1 = SCNSphere(radius: 0.1)
        point1.firstMaterial?.diffuse.contents = UIColor.blue
        node1 = SCNNode(geometry: point1)
        node1.position = SCNVector3(0, 0, -0.2)

        point2 = SCNSphere(radius: 0.1)
        point2.firstMaterial?.diffuse.contents = UIColor.black
        node2 = SCNNode(geometry: point2)
        node2.position = SCNVector3(-0.2, 0, 0)

        point3 = SCNSphere(radius: 0.1)
        point3.firstMaterial?.diffuse.contents = UIColor.yellow
        node3 = SCNNode(geometry: point3)
        node3.position = SCNVector3(0, -0.2, 0)

        point4 = SCNSphere(radius: 0.1)
        point4.firstMaterial?.diffuse.contents = UIColor.red
        node4 = SCNNode(geometry: point4)
        node4.position = SCNVector3(0, 0, 0.2)

        point5 = SCNSphere(radius: 0.1)
        point5.firstMaterial?.diffuse.contents = UIColor.green
        node5 = SCNNode(geometry: point5)
        node5.position = SCNVector3(0.2, 0, 0)

        point6 = SCNSphere(radius: 0.1)
        point6.firstMaterial?.diffuse.contents = UIColor.orange
        node6 = SCNNode(geometry: point6)
        node6.position = SCNVector3(0, 0.2, 0)


        sceneView.scene.rootNode.addChildNode(node1)
        sceneView.scene.rootNode.addChildNode(node2)
        sceneView.scene.rootNode.addChildNode(node3)
        sceneView.scene.rootNode.addChildNode(node4)
        sceneView.scene.rootNode.addChildNode(node5)
        sceneView.scene.rootNode.addChildNode(node6)
    }

    var a = 0
    public func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        if a <= 100 {
            node1.position.y += 0.01
            node2.position.x += 0.01
            node3.position.z += 0.01
            node4.position.y += 0.01
            node5.position.y += 0.01
            node6.position.z += 0.01
        }else {
            node1.position.y -= 0.01
            node2.position.x += 0.01
            node3.position.z += 0.01
            node4.position.y += 0.01
            node5.position.y += 0.01
            node6.position.z += 0.01
        }
        a += 1
        if a == 200 {
            a = 0
        }
    }

    public func session(_ session: ARSession, didFailWithError error: Error) {}
    public func sessionWasInterrupted(_ session: ARSession) {}
    public func sessionInterruptionEnded(_ session: ARSession) {}
}
PlaygroundPage.current.liveView = HomeViewController()
PlaygroundPage.current.needsIndefiniteExecution = true
© www.soinside.com 2019 - 2024. All rights reserved.