这是实现它的一种方法:
在你的 ViewController.swift 中,定义
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAd:", name: "gameStateOff", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideAd:", name: "gameStateOn", object: nil)
使用这些在您的场景和视图控制器之间进行通信。
接下来,实现 showAd 和 hideAd 函数,当您的场景进入或离开“游戏状态”时将调用它们:
func showAd(notif: NSNotification) {
// this func places the ad to the bottom of the screen
var frame = ad.frame
frame.origin.x = view.bounds.width / 2 - frame.size.width / 2
frame.origin.y = view.bounds.height - frame.size.height
ad.frame = frame
}
func hideAd(notif: NSNotification) {
// this func places the ad outside the screen
var frame = ad.frame
frame.origin.x = view.bounds.width / 2 - frame.size.width / 2
frame.origin.y = -frame.size.height
ad.frame = frame
}
其中 ad 是您的 GADBannerView(adSize: kGADAdSizeBanner)。
接下来将其添加到您开始游戏的位置
NSNotificationCenter.defaultCenter().postNotificationName("gameStateOn", object: nil)
此调用使视图控制器执行 hideAd 功能。
当您想再次展示广告时,请致电
NSNotificationCenter.defaultCenter().postNotificationName("gameStateOff", object: nil)