【问题标题】:How to present a UiView from a SKScene in Swift如何在 Swift 中从 SKScene 呈现 UiView
【发布时间】:2019-01-27 07:32:15
【问题描述】:

我正在制作一个游戏,我想从我的MenuScene.swift(这是一个 SKScene)文件中展示一个UIView(用于我游戏中的共享场景)。我在 Stack Overflow 上搜索过类似的问题,但没有一个在我的游戏中起作用。谁能告诉我它是如何完成的?

菜单场景:

class MenuScene: SKScene {   
    override func didMove(to view: SKView) {
        backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0)
        addLabelsAndButtons()
    }

    func addLabelsAndButton() {

        let shareButton = SKSpriteNode(imageNamed: "ShareButton")
        shareButton.size = CGSize(width: frame.size.height/12, height: frame.size.height/12)
        shareButton.position = CGPoint(x: frame.midX + shareButton.size.width/1.5, y: frame.minY + shareButton.size.height)
        shareButton.name = "ShareButton"
        addChild(shareButton)

    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if atPoint((touch.location(in: self))).name == "ShareButton" {
            //Where I want to present my Share Scene!
        }
    } 
}

【问题讨论】:

    标签: ios swift uiview skscene


    【解决方案1】:

    您不能在 SKScene 中呈现 UIView,因为 SKScene 已经过优化,可以处理游戏的精灵渲染,并使用与 UIView 不同的技术堆栈。

    您可以做的是将一个 UIView 添加到添加了 SKView 的父视图(这也是一个 UIView)。这允许您在游戏的 sprite 视图之上呈现更加静态的视图,例如:分数视图、hud 视图或菜单。

    我在下面放了一张图:

    UIView
      ⮑ 0: SKView
              ⮑ 0: SKScene
      ⮑ 1: UIView
    

    【讨论】:

    • 首先感谢您的帮助!我现在会尽力做到你说的!
    • 但我的意思是,当在 MenuScene 中按下分享按钮时,它会呈现一个 UIView(属于分享 UIView)。但我不知道如何在它们之间进行过渡。有办法吗?我在 Stack Overflow 上见过很多类似的情况,但在我的游戏中都没有。
    • 听起来你想要一个委托设置或使用通知将你的意图传达给 UI 层。
    猜你喜欢
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    相关资源
    最近更新 更多