我有一个GameViewController,它在不同的场景之间转换,例如:CircleScene到SquareScene,back&转型。
所有上述逻辑都可以正常工作,只要GameViewController是"初始视图控制器"。
一旦我有另一个视图控制器,在GameViewController上方,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方法(现在/显示)失败。 ViewController演示文稿是否有任何错误导致场景转换失败?感谢您的帮助。