1

たとえば、CircleScene から SquareScene へ、Transitions で前後に遷移する GameViewController があります。

GameViewController が「初期ビュー コントローラー」である限り、上記のロジックはすべて正常に機能します。

GameViewController の上にもう 1 つのビュー コントローラーがあると、GameViewController の画面遷移が機能しなくなります。

GameViewController.swift

import UIKit
import SpriteKit
import GameplayKit

protocol ScreenManager {
    func gotoSquare()
    func gotoCircle()
}

class GameViewController: UIViewController, ScreenManager {

    let sceneHeight = 380
    let sceneWidth = 570

    func gotoCircle() {
        let transition = SKTransition.push(with: .right, duration: 1.0)
        let newScene = CircleScene(size: CGSize(width: sceneWidth, height: sceneHeight))
        newScene.screenManager = self
        newScene.scaleMode = .aspectFill
        let view = self.view as! SKView?
        view?.presentScene(newScene, transition: transition)
    }

    func gotoSquare() {
        let transition = SKTransition.push(with: .left, duration: 1.0)
        let newScene = SquareScene(size: CGSize(width: sceneWidth, height: sceneHeight))
        newScene.screenManager = self
        newScene.scaleMode = .aspectFill
        let view = self.view as! SKView?
        view?.presentScene(newScene, transition: transition)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if let view = self.view as! SKView? {

            let scene = CircleScene(size: CGSize(width: sceneWidth, height: sceneHeight))
            scene.screenManager = self

            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)

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

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .landscape
        } else {
            return .all
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }
}

MainViewContoller.swift

import UIKit

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnPressed(_ sender: AnyObject) {

        // Go to GameViewController
        let gameViewController:GameViewController = storyboard?.instantiateViewController(withIdentifier: "GameViewControllerID") as! GameViewController

        self.present(gameViewController, animated:false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .landscape
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

ViewController メソッド (present/show) を試しましたが失敗しました。シーンの遷移が失敗する原因となる ViewController のプレゼンテーションに問題はありますか? あなたの助けに感謝。

4

0 に答える 0