如何使用情节提要停止当前模态转场的 ARView 会话?
How do I stop ARView session on present modally segue using storyboard?
我有两个包含 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 { [=10=] as? ARImageAnchor }.forEach {
let anchorEntity = AnchorEntity()
let modelEntity = fvBoatAnchor.fvBoatObject!
anchorEntity.addChild(modelEntity)
arView.scene.addAnchor(anchorEntity)
anchorEntity.transform.matrix = [=10=].transform
imageAnchorToEntity[[=10=]] = anchorEntity
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
anchors.compactMap { [=10=] as? ARImageAnchor }.forEach {
let anchorEntity = imageAnchorToEntity[[=10=]]
anchorEntity?.transform.matrix = [=10=].transform
}
}
func installGestures(on object:ModelEntity){
object.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation,.scale], for: object)
}
}
两个视图控制器的代码与上面相同。
每当我使用 Present Modally segue 导航到下一个 ARview 时,我的帧率都会显着下降。当我转到下一个 ARview 时,如何确保 ARview 会话关闭?
Storyboard view of the ViewController
也试过添加这个功能,但不知道为什么不行...
func leaveScene() {
arView?.session.pause()
arView?.removeFromSuperview()
arView = nil
}
您没有关闭所有需要关闭的东西。
func leaveScene() {
arView?.session.pause()
arView?.session.delegate = nil
arView?.scene.anchors.removeAll()
arView?.removeFromSuperview()
arView?.window?.resignKey()
arView = nil
}
P.S。但是 arView 不会从内存中释放。
这帮我解决了。使用来自@Andy 的代码,我添加了 leaveScene 函数并将其称为使用来自 UIbutton 的发送事件:
@IBAction func leaveScene(_ sender: UIbutton) {
leaveScene()
}
这让我的代码看起来像这样。
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 { [=11=] as? ARImageAnchor }.forEach {
let anchorEntity = AnchorEntity()
let modelEntity = fvBridgeAnchor.fvBridgeObject!
anchorEntity.addChild(modelEntity)
arView.scene.addAnchor(anchorEntity)
anchorEntity.transform.matrix = [=11=].transform
imageAnchorToEntity[[=11=]] = anchorEntity
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
anchors.compactMap { [=11=] as? ARImageAnchor }.forEach {
let anchorEntity = imageAnchorToEntity[[=11=]]
anchorEntity?.transform.matrix = [=11=].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()
}
}
我有两个包含 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 { [=10=] as? ARImageAnchor }.forEach {
let anchorEntity = AnchorEntity()
let modelEntity = fvBoatAnchor.fvBoatObject!
anchorEntity.addChild(modelEntity)
arView.scene.addAnchor(anchorEntity)
anchorEntity.transform.matrix = [=10=].transform
imageAnchorToEntity[[=10=]] = anchorEntity
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
anchors.compactMap { [=10=] as? ARImageAnchor }.forEach {
let anchorEntity = imageAnchorToEntity[[=10=]]
anchorEntity?.transform.matrix = [=10=].transform
}
}
func installGestures(on object:ModelEntity){
object.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation,.scale], for: object)
}
}
两个视图控制器的代码与上面相同。
每当我使用 Present Modally segue 导航到下一个 ARview 时,我的帧率都会显着下降。当我转到下一个 ARview 时,如何确保 ARview 会话关闭?
Storyboard view of the ViewController
也试过添加这个功能,但不知道为什么不行...
func leaveScene() {
arView?.session.pause()
arView?.removeFromSuperview()
arView = nil
}
您没有关闭所有需要关闭的东西。
func leaveScene() {
arView?.session.pause()
arView?.session.delegate = nil
arView?.scene.anchors.removeAll()
arView?.removeFromSuperview()
arView?.window?.resignKey()
arView = nil
}
P.S。但是 arView 不会从内存中释放。
这帮我解决了。使用来自@Andy 的代码,我添加了 leaveScene 函数并将其称为使用来自 UIbutton 的发送事件:
@IBAction func leaveScene(_ sender: UIbutton) {
leaveScene()
}
这让我的代码看起来像这样。
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 { [=11=] as? ARImageAnchor }.forEach {
let anchorEntity = AnchorEntity()
let modelEntity = fvBridgeAnchor.fvBridgeObject!
anchorEntity.addChild(modelEntity)
arView.scene.addAnchor(anchorEntity)
anchorEntity.transform.matrix = [=11=].transform
imageAnchorToEntity[[=11=]] = anchorEntity
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
anchors.compactMap { [=11=] as? ARImageAnchor }.forEach {
let anchorEntity = imageAnchorToEntity[[=11=]]
anchorEntity?.transform.matrix = [=11=].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()
}
}