这是一种从后台模式返回后保持视图暂停的方法。
Xpre 7 (有关Xpre 8的说明,请参见下文)
在情节提要中,
1)将视图的类更改为MyView
在视图控制器中,
2)用一个名为stayPaused的布尔值定义一个SKView子类
class MyView: SKView { var stayPaused = false override var paused: Bool { get { return super.paused } set { if (!stayPaused) { super.paused = newValue } stayPaused = false } } func setStayPaused() { if (super.paused) { self.stayPaused = true } }}3)将视图定义为MyView
4)添加一个通知程序来设置stayPaused标志
class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { // Configure the view. let skView = self.view as MyView NSNotificationCenter.defaultCenter().addObserver(skView, selector:Selector("setStayPaused"), name: "stayPausedNotification", object: nil)在应用程序委托中,
5)发布通知以设置应用程序处于活动状态时的停留暂停标志
func applicationDidBecomeActive(application: UIApplication) { NSNotificationCenter.defaultCenter().postNotificationName("stayPausedNotification", object:nil)}Xpre 8
在情节提要中,
1)将视图的类别从更改
SKView为
MyView
在视图控制器中,
2)定义一个
SKView带有名为stayPaused的布尔值的子类
class MyView: SKView { var stayPaused = false override var isPaused: Bool { get { return super.isPaused } set { if (!stayPaused) { super.isPaused = newValue } stayPaused = false } } func setStayPaused() { if (super.isPaused) { self.stayPaused = true } }}3)将视图定义为MyView
4)添加一个通知程序来设置stayPaused标志
class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() if let view = self.view as! MyView? { NotificationCenter.default.addObserver(view, selector:#selector(MyView.setStayPaused), name: NSNotification.Name(rawValue: "stayPausedNotification"), object: nil)在应用程序委托中,
5)发布通知以设置应用程序处于活动状态时的停留暂停标志
func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "stayPausedNotification"), object: nil)}


