使用SKScene呈现ViewController时暂停

时间:2015-10-21 18:52:06

标签: ios swift sprite-kit

编辑:好的,即使是一个空的SpriteKit场景也会发生这种情况! 这可能有什么问题? 为什么iOS需要2秒才能显示SpriteKit场景?

编辑2:我第一次点击标签栏以显示带有SKScene的视图控制器,它立即显示,但每次下次我尝试导航回此视图控制器时,它将花费2秒钟显示!

我的应用中有一个带有多个不同viewControllers的标签栏控制器。

其中一个包含一个SpriteKit场景,有几个节点,其中5个节点,没有太重。

在模拟器中一切都很好,但是当在设备上进行测试时,我注意到在使用SpriteKit场景更改为ViewController时,有时会出现大停顿(大约2秒!)。

其他时间没有停止,立即显示视图。

现在,我知道我必须在这里做错事,因为iOS绝对应该能够解决这个问题。

这是viewController里面的viewDidLoad函数和spriteKit场景:

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = MyScene(fileNamed:"MyScene") {
        // Configure the view.
        scene.switchView = switchView

        let parentNode = scene.childNodeWithName("WholeObject") as! SKSpriteNode
        let contentNode = scene.childNodeWithName("CenterNode") as! SKSpriteNode
        addPhotoToFrame(contentNode, photoName: "woman", maskName: "circleMask")

        let node1 = parentNode.childNodeWithName("node1")  as! SKSpriteNode
        addPhotoToFrame(zealNode1, photoName: "motherCircleImage", maskName: "circleMaskSmall")

        let node2 = parentNode.childNodeWithName("node2")  as! SKSpriteNode
        addPhotoToFrame(zealNode2, photoName: "hairstylistCircleImage", maskName: "circleMaskSmall")

        let node3 = parentNode.childNodeWithName("node3")  as! SKSpriteNode
        addPhotoToFrame(zealNode3, photoName: "dietCircleImage", maskName: "circleMaskSmall")


        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true


        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

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


        skView.presentScene(scene)
    }

}

2 个答案:

答案 0 :(得分:1)

嗨deloki我在swift中创建了一个新项目并提供了一个解决方案,它在设备上工作正常.....看看我的代码

这是我的GameViewController,它调用GameScene

import UIKit
import SpriteKit

extension SKNode {
    class func unarchiveFromFile1(file : String) -> SKNode? {
        if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
            var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
            var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

            archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
            let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
            archiver.finishDecoding()
            return scene
        } else {
            return nil
        }
    }
}

class GameViewController: UIViewController {
       var skView:SKView!
    override func viewDidLoad() {

        super.viewDidLoad()

        if let scene = GameScene.unarchiveFromFile1("GameScene") as? GameScene {
            // Configure the view.
            let graphRect:CGRect = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
             skView = SKView();
             skView.frame=graphRect
            skView.showsFPS = true
            skView.showsNodeCount = true
            self.view.addSubview(skView)
            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

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

            skView.presentScene(scene)
            let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
            dispatch_after(delayTime, dispatch_get_main_queue()) {
                let s2=GameViewController1();
                self.presentViewController(s2, animated: true, completion: { () -> Void in
                    //
                })
            }

        }
    }
    override func viewDidDisappear(animated: Bool) {
        if((skView) != nil)
        {
            skView .removeFromSuperview();
            skView=nil;
        }

    }
    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
    }
}

这是我的GameViewController1,它调用MyScene1

import UIKit
import SpriteKit

extension SKNode {
    class func unarchiveFromFile(file : String) -> SKNode? {
        if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
            var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
            var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

            archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
            let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
            archiver.finishDecoding()
            return scene
        } else {
            return nil
        }
    }
}

class GameViewController1: UIViewController {
      var skView:SKView!
    override func viewDidLoad() {

        super.viewDidLoad()

        if let scene = GameScene.unarchiveFromFile("MyScene1") as? GameScene {
            // Configure the view.
            let graphRect:CGRect = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)
            skView=SKView()
            skView.frame=graphRect
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
           // scene.scaleMode = .AspectFill
            self.view.addSubview(skView)
            skView.presentScene(scene)
            let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
            dispatch_after(delayTime, dispatch_get_main_queue()) {
                let s2=GameViewController();
                self.presentViewController(s2, animated: true, completion: { () -> Void in
                    //
                })
            }
        }
    }

    override func viewDidDisappear(animated: Bool) {
        if((skView) != nil)
        {
            skView .removeFromSuperview();
            skView=nil;
        }

    }
    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
    }
}

从故事板创建SKview我刚从程序化创建它,每2秒我从一个视图切换到另一个视图并且它在所有设备上工作正常但是有一件事我使用了Xcode 6.4和ios9  您可以从http://www.filedropper.com/tryit

下载链接

答案 1 :(得分:1)

我不知道是什么触发了这个问题,但是在视图消失之前暂停,为我修复了它。

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.contentView.paused = false
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.contentView.paused = true
}