我在游戏中按钮暂停,效果很好! 但是当我再次从应用程序或午餐应用程序退出时,我需要暂停我的游戏。
我希望当我触摸按钮时屏幕打开暂停将在我返回游戏时打开
如何正确地做到这一点?
答案 0 :(得分:1)
你应该看到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
})
}
并创建新的swift文件并添加此代码
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
}
}
}
}
}