我希望有人能告诉我为什么会发生这种情况以及如何解决它。在 SwiftUI ContentView 中,我使用 SpriteView() 来呈现 SKScene。
struct ContentView: View {
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject var appData : AppData
@ObservedObject var viewModel = ViewModel()
var gameScene: SKScene {
let scene = GameScene(size: CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
scene.viewModel = self.viewModel
scene.scaleMode = .aspectFill
return scene
}
var summaryScene: SKScene {
let scene = SummaryScene(size: CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
scene.viewModel = self.viewModel
scene.scaleMode = .aspectFill
return scene
}
var body: some View {
ZStack{
GeometryReader { geometry in
SpriteView( scene: chooseScene(for: viewModel.complete))
.frame(width: geometry.size.width, height: geometry.size.height)
}
.edgesIgnoringSafeArea(.all)
VStack{
...
}
}
}
func chooseScene(for complete: Bool) -> {
complete ? summaryScene : gameScene
}
GameScene中的相关代码如下:
class GameScene: SKScene {
...
override func didMove(to view: SKView) {
print("did move to view")
backgroundColor = UIColor(red: 30 / 255, green: 32 / 255, blue: 53 / 255, alpha: 1.0)
layoutScene()
startCounter()
}
print 语句告诉我 didMove(to view) 被调用了两次,它导致屏幕上出现一个非常短暂的闪烁。否则,一切正常。我调用 SKScene 的方式是否存在明显问题?还是我应该在别处寻找原因?谢谢。