首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在当前使用故事板的模式下停止ARView会话?

如何在当前使用故事板的模式下停止ARView会话?
EN

Stack Overflow用户
提问于 2022-03-13 18:58:46
回答 2查看 181关注 0票数 0

我有两个ViewControllers包含一个ARView。守则如下:

代码语言:javascript
复制
import UIKit
import RealityKit
import ARKit

class fvBoat: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
        let fvBoatAnchor = try! Vard.loadFvBoatScene()
            var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
    
           
        override func viewDidLoad() {
                super.viewDidLoad()
            fvBoatAnchor.generateCollisionShapes(recursive: true)
            let fvBoat = fvBoatAnchor.fvBoatObject as? Entity & HasCollision
                arView.installGestures(for: fvBoat!)
                arView.scene.addAnchor(fvBoatAnchor)
                arView.session.delegate = self
        }
           
            func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = AnchorEntity()
                    let modelEntity = fvBoatAnchor.fvBoatObject!
                    anchorEntity.addChild(modelEntity)
                    arView.scene.addAnchor(anchorEntity)
                    anchorEntity.transform.matrix = $0.transform
                    imageAnchorToEntity[$0] = anchorEntity
                }
            }

            func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = imageAnchorToEntity[$0]
                    anchorEntity?.transform.matrix = $0.transform
                }
            }
            func installGestures(on object:ModelEntity){
                
                object.generateCollisionShapes(recursive: true)
                arView.installGestures([.rotation,.scale], for: object)
            }
    }

两个视图控制器都具有与上面相同的代码。

每当我使用一个当前模式索引导航到下一个ARview时,我的框架就会显著下降。如何确保在进入下一个ARview时关闭ARview会话?

ViewController的故事板视图

也尝试过添加这个函数,但不确定为什么它不能工作.

代码语言:javascript
复制
func leaveScene() {

        arView?.session.pause()
        arView?.removeFromSuperview()
        arView = nil

    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-14 09:56:45

不是所有需要关闭的东西你都关掉了。

代码语言:javascript
复制
func leaveScene() {

    arView?.session.pause()
    arView?.session.delegate = nil
    arView?.scene.anchors.removeAll()
    arView?.removeFromSuperview()
    arView?.window?.resignKey()
    arView = nil
}

但是arView不会从内存中被释放。

票数 1
EN

Stack Overflow用户

发布于 2022-03-14 15:43:21

这为我解决了问题。使用@Andy的代码,我添加了leaveScene函数并将其调用为使用来自UIbutton的发送事件:

代码语言:javascript
复制
@IBAction func leaveScene(_ sender: UIbutton) {
        leaveScene()
    }

让我的代码看起来像这样。

代码语言:javascript
复制
import UIKit
import RealityKit
import ARKit

class fvBridge: UIViewController, ARSessionDelegate {
    
    @IBOutlet var arView: ARView!
    
        let fvBridgeAnchor = try! Experience.loadFvBridgeScene()
            var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
    
           
        override func viewDidLoad() {
                super.viewDidLoad()
            fvBridgeAnchor.generateCollisionShapes(recursive: true)
            let fvBridge = fvBridgeAnchor.fvBridgeObject as? Entity & HasCollision
                arView.installGestures(for: fvBridge!)
                arView.scene.addAnchor(fvBridgeAnchor)
                arView.session.delegate = self
        }
           
            func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = AnchorEntity()
                    let modelEntity = fvBridgeAnchor.fvBridgeObject!
                    anchorEntity.addChild(modelEntity)
                    arView.scene.addAnchor(anchorEntity)
                    anchorEntity.transform.matrix = $0.transform
                    imageAnchorToEntity[$0] = anchorEntity
                }
            }

            func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
                anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                    let anchorEntity = imageAnchorToEntity[$0]
                    anchorEntity?.transform.matrix = $0.transform
                }
            }
            func installGestures(on object:ModelEntity){
                
                object.generateCollisionShapes(recursive: true)
                arView.installGestures([.rotation,.scale], for: object)
            }
    func leaveScene() {

        arView?.session.pause()
        arView?.session.delegate = nil
        arView?.scene.anchors.removeAll()
        arView?.removeFromSuperview()
        arView?.window?.resignKey()
        arView = nil
    }
    
    @IBAction func leaveScene(_ sender: Any) {
        leaveScene()
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71459891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档