Search code examples
iosswiftsprite-kitskscene

How to change SKScene size when rotate Device?


Im trying to make a SKScene that renders a fractal tree using a recursive functions. Each branch is a SKShapeNode. The initial line length should always be a percentage of the scene height. I have a computed variable used for the first line length that currently returns frame.height * 0.3. My problem is that I want to keep the percentage right when the device is rotated. I added the following code to my ViewController:

    var scene: FractalTreeScene!
    override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
        if fromInterfaceOrientation == .landscapeLeft || fromInterfaceOrientation == .landscapeRight {
            scene.size = CGSize(width: 1337, height: 750)
        } else {
            scene.size = CGSize(width: 750, height: 1337)
        }
        scene.drawTree()
    }
    override func viewDidLoad() {
        super.viewDidLoad()


        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            scene = FractalTreeScene()
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill
            if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
                scene.size = CGSize(width: 1337, height: 750)
            } else {
                scene.size = CGSize(width: 750, height: 1337)
            }
            // Present the scene
            view.presentScene(scene)

            view.ignoresSiblingOrder = true

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

I then launched the app in portrait and it all looked fine, then I tried rotating to landscape and the size didn't change at all(I added print(size) to update function on scene). I then rotated back to portrait and the size changed to what it should have changed when in landscape rotating back to landscape got me the portrait size.

I then restarted the app in portrait rotated to landscape right and as expected the size didn't change then I rotated to landscape left and the size changed to the right value.

So apparently the codes work but the size only really gets changed when I rotate scene after changing it. Is there a way to make this change instantly? or maybe is there a better way of changing scene size when rotating? or even is there a way to maintain line size and position relative to screen without changing scene size?


Solution

  • I fixed it by changing the code to :

     var scene: FractalTreeScene!
        override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    
            if UIApplication.shared.statusBarOrientation.isLandscape {
                scene.size = CGSize(width: 1337, height: 750)
            } else {
                scene.size = CGSize(width: 750, height: 1337)
            }
            scene.drawTree()
        }
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            if let view = self.view as! SKView? {
                // Load the SKScene from 'GameScene.sks'
                scene = FractalTreeScene()
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill
    
                if UIApplication.shared.statusBarOrientation.isLandscape{
                    scene.size = CGSize(width: 1337, height: 750)
                } else {
                    scene.size = CGSize(width: 750, height: 1337)
                }
                // Present the scene
                view.presentScene(scene)
    
                view.ignoresSiblingOrder = true
    
                view.showsFPS = true
                view.showsNodeCount = true
            }
        }