栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在应用启动/退出时暂停Spritekit游戏。.iOS8

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在应用启动/退出时暂停Spritekit游戏。.iOS8

这是一种从后台模式返回后保持视图暂停的方法。

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号