我在游戏中有按钮暂停,它工作得很好!但当我再次退出应用程序或午餐应用程序时,我需要我的游戏暂停。
我希望屏幕是打开的,当我点击按钮时,暂停会在我返回游戏时打开
怎样才能做好呢?
发布于 2015-10-11 18:58:33
您应该看到https://github.com/aaronabentheuer/AAWindow这个项目。首先,您应该添加打开的AppDelegate.swift和
var window: UIWindow? = {
let window = AAWindow(frame: UIScreen.mainScreen().bounds, cornerRadius: 8)
return window
}()之后添加您的项目。
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
})
}并创建新的快速文件并添加以下代码
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
}
}
}
}}
https://stackoverflow.com/questions/33066941
复制相似问题