如何使用情节提要停止当前模态 Segue 上的 ARView 会话?
我有两个包含 ARView 的 ViewController。代码如下:
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)
}
}
两个视图控制器的代码与上面相同。
每当我使用 Present Modally segue 导航到下一个 ARview 时,我的帧速率就会显着下降。当我转到下一个 ARview 时,如何确保 ARview 会话关闭?
ViewController 的 Storyboard 视图
也尝试添加此功能,但不确定为什么它不起作用...
func leaveScene() {
arView?.session.pause()
arView?.removeFromSuperview()
arView = nil
}
I've got two ViewControllers containing an ARView. Code is as follows:
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)
}
}
Both view controller has the same code as above.
Whenever I navigate to the next ARview using a Present Modally segue, my framerate drops significantly. How do I make sure that ARview session is closed down when I segue to the next ARview?
Storyboard view of the ViewController
Also tried adding this function, but not sure why it doesn't work...
func leaveScene() {
arView?.session.pause()
arView?.removeFromSuperview()
arView = nil
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您并未关闭所有需要关闭的内容。
PS 但 arView 不会从内存中释放。
You turned off not everything that needed to be turned off.
P.S. But arView will not be deallocated from memory.
这为我解决了这个问题。使用@Andy 的代码,我添加了leaveScene 函数,并将其称为使用UIbutton 中的发送事件:
这使我的代码如下所示。
This solved it for me. Using the code from @Andy I added the leaveScene function and called it as using send event from the UIbutton:
Which make my code look like this.