Monkeying Around With SpriteKit—Embedding UIViews in SKViews


import UIKit
import SpriteKit
import GameplayKit

class myFirstScene: SKScene {
    
    var vWidth:CGFloat {
        return (self.view?.frame.size.width)!
    }
    var vHeight:CGFloat {
        return (self.view?.frame.size.height)!
    }
    
    override func didMove(to skView: SKView) {
        let uiVIew: UIView = UIView(frame: CGRect(x: 0, y: vHeight * 0.25, width: (vWidth * 0.5), height: (vHeight * 0.5)))
        uiVIew.backgroundColor = UIColor.blue

        let txtView = UITextView(frame: uiVIew.bounds)
        txtView.text = "I am a UITextView \nIn a UI View\nIn a SKView \nIn an SKScene\nDrien by a UIViewController."
        txtView.textColor = UIColor.white
        txtView.textAlignment = .left
        txtView.isEditable = false
        txtView.backgroundColor = UIColor.clear
        
        uiVIew.addSubview(txtView)
        skView.addSubview(uiVIew)
    }
}

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let skView = self.view as! SKView
        let mainViewFrame:CGRect = self.view.bounds
        let skScene = myFirstScene(size: mainViewFrame.size)
        
        skView.presentScene(skScene)
    }
}

Bookmark and Share