У меня возникла проблема при создании SpriteKit
сцены в SwiftUI
. Первоначально я создал этот проект как SwiftUI
проект.
Вот код, который у меня есть:
ContentView.swift:
/// Where the UI content from SwiftUI originates from.
struct ContentView : View {
var body: some View {
// Scene
SceneView().edgesIgnoringSafeArea(.all)
}
}
SceneView.swift:
/// Creates an SKView to contain the GameScene. This conforms to UIViewRepresentable, and so can be used within SwiftUI.
final class SceneView : SKView, UIViewRepresentable {
// Conformance to UIViewRepresentable
func makeUIView(context: Context) -> SKView {
print("Make UIView")
return SceneView(frame: UIScreen.main.bounds)
}
func updateUIView(_ uiView: SKView, context: Context) {
print("Update UIView")
}
// Creating scene
override init(frame: CGRect) {
super.init(frame: frame)
let scene = Scene(size: UIScreen.main.bounds.size)
presentScene(scene)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Scene.swift:
/// The scene for the game in SpriteKit.
final class Scene : SKScene {
override func didMove(to view: SKView) {
super.didMove(to: view)
print("Scene didMove:")
}
}
Задача
Проблема в том, что сцена перезагружается несколько раз, как показано в журналах (поскольку в коде print
s):
Сцена didMove:
Make UIView
Сцена didMove:
Обновление UIView
Как видите, Scene didMove:
печатается дважды. Я хочу, чтобы это вызывалось только один раз, так как я хочу создать свои спрайты здесь. Есть идеи?