首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >暂停并继续玩app午餐/退出app ( SpriteKit Swift)

暂停并继续玩app午餐/退出app ( SpriteKit Swift)
EN

Stack Overflow用户
提问于 2015-10-11 15:51:28
回答 1查看 544关注 0票数 0

我在游戏中有按钮暂停,它工作得很好!但当我再次退出应用程序或午餐应用程序时,我需要我的游戏暂停。

我希望屏幕是打开的,当我点击按钮时,暂停会在我返回游戏时打开

怎样才能做好呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-11 18:58:33

您应该看到https://github.com/aaronabentheuer/AAWindow这个项目。首先,您应该添加打开的AppDelegate.swift和

代码语言:javascript
复制
var window: UIWindow? = {
    let window = AAWindow(frame: UIScreen.mainScreen().bounds, cornerRadius: 8)
    return window
    }()

之后添加您的项目。

代码语言:javascript
复制
func NC()

{
    NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification, object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: { notification in
    })
    NSNotificationCenter.defaultCenter().addObserverForName("applicationWillResignActiveWithoutControlCenter", object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: { notification in
        //code
    })
}
func CC()
{
    NSNotificationCenter.defaultCenter().addObserverForName("applicationWillResignActiveWithControlCenter", object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: { notification in
  //code
    })
}
func opened()
{
    NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification, object: nil, queue: NSOperationQueue.mainQueue(), usingBlock: { notification in
        // code
    })
}

并创建新的快速文件并添加以下代码

代码语言:javascript
复制
import UIKit

class AAWindow: UIWindow {


private var activeCornerRadius : CGFloat = 0
private var inactiveCornerRadius : CGFloat = 0
private var cornerRadiusAnimationDuration : Double = 0.15

private var willOpenControlCenter : Bool = false
private var controlCenterOpened : Bool = false
var timer : NSTimer = NSTimer()

private var applicationWillResignActiveWithControlCenterNotification = NSNotification(name: "applicationWillResignActiveWithControlCenter", object: nil)

private var applicationWillResignActiveWithoutControlCenterNotification = NSNotification(name: "applicationWillResignActiveWithoutControlCenter", object: nil)

init(frame: CGRect, cornerRadius: Float) {
    super.init(frame: frame)

    self.clipsToBounds = true
    self.layer.cornerRadius = inactiveCornerRadius

    activeCornerRadius = CGFloat(cornerRadius)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationDidBecomeActive:", name: UIApplicationDidBecomeActiveNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationWillResignActive:", name: UIApplicationWillResignActiveNotification, object: nil)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

@objc private func applicationDidBecomeActive (notification : NSNotification) {

    if (controlCenterOpened) {
        controlCenterOpened = false
    } else {
        self.layer.cornerRadius = activeCornerRadius

    }
}
@objc private func applicationWillResignActive (notification : NSNotification) {

    if (willOpenControlCenter) {

        NSNotificationCenter.defaultCenter().postNotification(applicationWillResignActiveWithControlCenterNotification)

        willOpenControlCenter = false
        controlCenterOpened = true
    } else {
        NSNotificationCenter.defaultCenter().postNotification(applicationWillResignActiveWithoutControlCenterNotification)

        self.layer.cornerRadius = inactiveCornerRadius
    }
}

private var touchLocation : CGPoint = CGPoint()

override func sendEvent(event: UIEvent) {
    super.sendEvent(event)

    if (event.type == UIEventType.Touches) {
        for touchevent in event.allTouches()! {
            let touch = touchevent

            if (touch.phase == UITouchPhase.Began && touch.locationInView(self).y - self.frame.height * 0.9 >= 0) {
                willOpenControlCenter = true
            }
        }
    }
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33066941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档