我正在使用Swift在SpriteKit中制作游戏,目前有两个viewControllers。第一个viewController是GameViewController,它使用故事板显示菜单,其中一些按钮位于界面构建器中。菜单上有一个播放按钮,转换到显示gameScene的第二个viewController。两个viewControllers加载SKScene。
要从一个视图控制器转换到另一个视图控制器,然后再返回,我将使用在故事板中创建的segues。
我遇到的问题是,当我在viewControllers之间切换导致应用程序最终崩溃时,内存会增加。我有一种感觉,viewControllers只是堆叠在一起。这是我的GameViewController和GameSceneViewController的代码。
GameViewController:
class GameViewController: UIViewController, GKGameCenterControllerDelegate {
//MARK: Variables
var scene: Menu!
@IBOutlet weak var settingsBtn: UIButton!
@IBOutlet weak var leaderboardsBtn: UIButton!
@IBOutlet weak var storeBtn: UIButton!
@IBOutlet weak var playBtn: UIButton!
@IBAction func play(sender: UIButton) {
}
//MARK: Button Animations
func menuButtonAnimations(){
settingsBtn.transform = CGAffineTransformMakeTranslation(0, 200)
leaderboardsBtn.transform = CGAffineTransformMakeTranslation(0, 200)
storeBtn.transform = CGAffineTransformMakeTranslation(0, 200)
springWithDelay(0.5, 0.7, {
self.settingsBtn.transform = CGAffineTransformMakeTranslation(0, 0)
})
springWithDelay(0.5, 0.75, {
self.leaderboardsBtn.transform = CGAffineTransformMakeTranslation(0, 0)
})
springWithDelay(0.5, 0.8, {
self.storeBtn.transform = CGAffineTransformMakeTranslation(0, 0)
})
}
//MARK: The View
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = false
skView.showsNodeCount = false
skView.showsPhysics = false
skView.ignoresSiblingOrder = true
scene = Menu(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
//Button Animation
menuButtonAnimations()
}
//MARK: Extra Stuff
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
GameSceneViewController:
class GameSceneViewController: UIViewController{
//MARK: Variables
@IBOutlet weak var returnBtn: UIButton!
//MARK: Helper Functions
//MARK: The View
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Configure the view.
var scene: GameScene!
let skView = self.view as SKView
skView.showsFPS = false
skView.showsNodeCount = false
skView.showsPhysics = false
skView.ignoresSiblingOrder = true
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
//Notifications
}
segues是使用故事板中的链接创建的模态segue。
如何在不增加内存的情况下在两个viewControllers之间来回切换的任何帮助都会很棒!
答案 0 :(得分:1)
如果一个视图控制器通过模态segue呈现另一个视图控制器,则应使用dismissViewControllerAnimated:completion:
返回第一个视图控制器,而不是堆叠模态演示文稿。
答案 1 :(得分:1)
我相信你可以做IBAction来解雇模态视图:
目标-C:
[self dismissViewControllerAnimated:YES completion:nil];
夫特:
self.dismissViewControllerAnimated(true, completion: nil)
我相信后退按钮可以从包含NSViewController的dismissController:
链接到解除模态视图(至少对于Cocoa故事板,无论如何)。