Kevin Wang
Kevin Wang

Reputation: 49

How to present a UiView from a SKScene in Swift

I am making a game, and I want to present a UIView (for my Share Scene in my game), from my MenuScene.swift (which is a SKScene) file. I've searched on Stack Overflow for similar questions, but none of them worked in my game. Can anyone show me how it's done?

Menu Scene:

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!
        }
    } 
}

Upvotes: 0

Views: 277

Answers (1)

Danny Bravo
Danny Bravo

Reputation: 4658

You cannot present a UIView inside an SKScene, as SKScene has been optimised to handle sprite rendering for games and uses a different technology stack from UIView.

What you can do is add a UIView to the parent view that the SKView was added to (which is also a UIView). This allows you to present a more static view overplayed on top of your game's sprite view, for example: a score view, hud view or a menu.

I've put a diagram of this below:

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

Upvotes: 1

Related Questions