swift 如何调用aUIView.presentScene(aSKScene,过渡:aSKTransition)是否与aUIView.addSubview(aUITextField!)一致?

dbf7pr2w  于 2023-01-01  发布在  Swift
关注(0)|答案(1)|浏览(80)
    • 如何调用aUIView. presentScene(aSKScene,过渡:aSKTransition)与aUIView. addSubview(aUITextField!)一致操作?**
      ***没有***转换,没有问题。*有*转换,UITextField滞后于转换-不在统一中。
    • 代码**
func showScene(theSceneName: String) {

    if let ourScene = SKScene(fileNamed: theSceneName) {
                                    
        addTextFieldToVC(toSceneName: theSceneName)
        // NB: theSceneName is passed by Reference so we can
        //     return here before we call presentScene(...)
        addGamePiecesToScene(toScene: theSceneName)

        if let theView = self.view as! SKView? {
            theView.ignoresSiblingOrder = true
            theView.showsFPS = true
            theView.showsNodeCount = true
                
            // Finally, present the scene
            let theTransition = SKTransition.doorway(withDuration: 2.0)
            theView.presentScene(ourScene, transition: theTransition)
        }
            
    }   // if let ourScene
        
}   // showScene

func addTextFieldToVC(toSceneName: String) {

    if (toSceneName == "GameScene") {
        if let theView = self.view as! SKView? {
            aUITextField = UITextField(frame:CGRectMake(x: aXValue, y: aYValue))
            theView.addSubview(aUITextField!)
        }
    }

}

func addGamePiecesToScene(toScene: SKScene) {

    myRoom = SKSpriteNode(texture: SKTexture(imageNamed: roomImg))
    myRoom!.zPosition = roomZposition
    // etc with .size, .position
    toScene.addChild(myRoom!)

}

如上所示,我首先添加UITextField,然后添加SKSpriteNode图像。
然而它们与SKTransition不同步,它们只是没有SKTransition才同步出现。
FWIW,我已经在showScene中尝试了这个序列,但没有任何变化:

theView.presentScene(theSceneName, transition: theTransition)
DispatchQueue.main.async {
    addTextFieldToVC(toSceneName: theSceneName)
}

我还开始试验完成处理程序:

override func viewDidLoad() {
        
    super.viewDidLoad()
                
    setupScene()
        
    // Completion Handler for showScene()
    showModifiedScene {
                 
        if thisSceneName == "GameScene" {  // a global var
            addTextFieldToVC(toSceneName: thisSceneName!)
        }
            
    }
     
}   // viewDidLoad

// modified for callback option??
func showScene(theSceneName: String) {

    if let ourScene = SKScene(fileNamed: theSceneName) {
                                    
        addGamePiecesToScene(toScene: ourScene)

        showModifiedScene()
            
    }   // if let ourScene
        
}   // showScene

func showModifiedScene(completionBlock: () -> Void) {

    if thisSceneName == "GameScene" {  // a global var
        if let ourScene = SKScene(fileNamed: thisSceneName!) {
            if let theView = self.view as! SKView? {
                theView.ignoresSiblingOrder = true
                theView.showsFPS = true
                theView.showsNodeCount = true
                    
                let theTransition = SKTransition.doorway(withDuration: 2.0)
                theView.presentScene(ourScene, transition: theTransition)
            }
        }

        completionBlock()
    }
                
}   // showModifiedScene

同样,***没有***转换,没有问题。*有*转换,UITextField滞后于转换-不在统一中。
我还在忍受着这个...但是我想是时候叫一些增援了!在我等待加略山的时候,我仍然会继续工作。

6pp0gazn

6pp0gazn1#

尝试添加文本视图,然后将对presentScene()的调用 Package 到对DispatchQueue.main.async()的调用中。
这样,文本字段将在您开始显示视图之前添加到视图中。

相关问题