为什么 makeCoordinator() 只调用一次?
ARViewContainer 有一个协调器,它由 makeCoordinator() 创建一次 ->协调员。该方法中的 print("makeCoordinator()") 告诉我,它只被调用一次。这是调试控制台中的第一个打印。我在 ARViewContainer 的初始化程序中也有一个 print("init") ,根据调试控制台,它被调用了很多次。
为什么 ARViewContainer 创建了很多次(正如预期的那样,因为 ContentView 在其主体中调用了 ARViewContainer()),但协调器只创建了一次?
import ARKit
import SwiftUI
import RealityKit
struct ARViewContainer: UIViewRepresentable {
init(coordinator: Coordinator) {
print("init")
}
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
config.environmentTexturing = .automatic
if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh){
config.sceneReconstruction = .mesh
}
arView.session.run(config)
arView.debugOptions = [
.showWorldOrigin,
.showSceneUnderstanding]
arView.session.delegate = context.coordinator
return arView
}
func makeCoordinator() -> Coordinator {
print("makeCoordinator()")
return Coordinator()
}
}
The ARViewContainer has a coordinator, which is created once by makeCoordinator() -> Coordinator
. A print("makeCoordinator()") in that method shows me, that it is only called once. It is the first print in the debug console. I also have a print("init") in the initializer of ARViewContainer, which is called many times according to the debug console.
Why is the ARViewContainer created many times (as expected, because the ContentView calls ARViewContainer()
in its body), but the coordinator is only made once?
import ARKit
import SwiftUI
import RealityKit
struct ARViewContainer: UIViewRepresentable {
init(coordinator: Coordinator) {
print("init")
}
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
config.environmentTexturing = .automatic
if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh){
config.sceneReconstruction = .mesh
}
arView.session.run(config)
arView.debugOptions = [
.showWorldOrigin,
.showSceneUnderstanding]
arView.session.delegate = context.coordinator
return arView
}
func makeCoordinator() -> Coordinator {
print("makeCoordinator()")
return Coordinator()
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论