【问题标题】:Display UIView behind SKScene in GameViewController在 GameViewController 中显示 SKScene 后面的 UIView
【发布时间】:2019-03-21 01:35:59
【问题描述】:

我有一个基本的 SpriteKit 游戏。

我基本上想做的是函数create_new_ui() 创建一个UIView 并将其隐藏在幕后。

override func viewDidLoad()
{
    super.viewDidLoad()

    create_new_ui()

    if let view = self.view as! SKView?
    {
        // Load the SKScene from 'GameScene.sks'
        let scene = MainScene()

        // Set the scale mode to scale to fit the window
        scene.scaleMode = .resizeFill
        scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)

        // Present the scene
        view.presentScene(scene)

        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true
    }
}

func create_new_ui()
{
    let ui_view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    ui_view.center = CGPoint(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 2)
    ui_view.backgroundColor = UIColor.red

    self.view.addSubview(ui_view)
}

您能告诉我如何将ui_view 移到SKScene 后面吗?

我试过sendSubviewToBack(_:),但没有效果。有什么想法吗?

【问题讨论】:

  • 添加新视图后尝试在 SKScene 上使用 bringSubviewToFront(_:)
  • 试过了。没有任何影响。
  • 你到底尝试了什么?
  • bringSubviewToFront(_:),返回,改变 layer.zPosition。如果我理解正确,视图只有一个我创建的子视图(ui_view)。场景不是子视图。
  • 是的,但是您在代码中的哪个位置尝试了这些?

标签: ios swift sprite-kit


【解决方案1】:

有几种方法可以做到这一点。一种方法是在视图出现后直接将其添加到窗口中。:

   override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    addFakeView()
   }

func addFakeView(){
    let fview = UIView.init(frame: CGRect.init(x: 100, y: 400, width: 100, height: 100))
    fview.backgroundColor = UIColor.green
    view.window?.insertSubview(fview, at: 0)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多