gpt4 book ai didi

ios - 使用 SKScene 呈现 ViewController 时暂停

转载 作者:IT王子 更新时间:2023-10-29 05:44:37 25 4
gpt4 key购买 nike

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

编辑 2:我第一次单击选项卡栏以显示带有 SKScene 的 View Controller 时,它会立即显示,但每次我尝试导航回该 View Controller 时,都需要 2 秒才能显示!

我的应用程序中有一个带有多个不同 View Controller 的标签栏 Controller 。

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

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

其他时候没有停止,立即显示 View 。

现在,我知道这里一定有什么地方做错了,因为 iOS 绝对应该能够处理这个问题。

这是我在带有 spriteKit 场景的 viewController 中的 viewDidLoad 函数:

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

}

最佳答案

嗨,deloki,我在 swift 中创建了一个新项目,并提供了一个在设备上运行良好的解决方案......检查我的代码

这是调用 GameScene 的 GameViewController

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

这是调用 MyScene1 的 GameViewController1

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

从 Storyboard创建 SKview 我只是通过编程创建它,每 2 秒我从一个 View 切换到另一个 View ,它在所有设备上工作正常,但有一件事我使用了 Xcode 6.4 和 ios9 你可以从http://www.filedropper.com/tryit下载链接

关于ios - 使用 SKScene 呈现 ViewController 时暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33267022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com