You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让UIView.presentScene(带SKTransition)与addSubview(UITextField)同步执行?

如何让UIView.presentScene(aSKScene, transition: aSKTransition)与UIView.addSubview操作同步执行?

不使用过渡动画时完全没有问题,但使用SKTransition时,UITextField会先出现,之后才完成SKTransition——二者不同步。

具体表现为:UITextField被添加到ViewController的view中后,会等待SKTransition完成。

代码示例

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: ourScene)

        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并不同步;只有不使用过渡动画时才同步。

已尝试的解决方案

调整执行顺序

曾在showScene中尝试以下顺序,但没有变化:

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

使用Completion Handler

尝试通过回调函数触发添加操作,结果依然无效:

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)
        thisSceneName = theSceneName
        showModifiedScene(completionBlock: {})
            
    }   // 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

同样,不使用过渡动画时完全没问题,但使用SKTransition时,UITextField会先出现,之后才完成SKTransition——二者不同步。

目前仍在尝试解决这个问题,希望寻求帮助,同时会继续尝试其他方法!


内容的提问来源于stack exchange,提问作者user20439709

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 06:25:11