如何在不同的 SpriteKit 场景中显示 iAd 横幅
How to display iAd banner in different SpriteKit Scenes
我用 Swift 和 SpriteKit 制作了简单的游戏。一切似乎都正常,但我的 iAd 设置有问题。我只想在特定场景(主菜单和游戏结束)而不是在游戏过程中显示广告横幅。
我的 iAd 设置有效,但前提是它一直显示。
我最后一次修复它的尝试是使用 NSNotificationCenter 方法。我已经按照我在其他 question/answers 此处看到的那样完成了,但应用程序在启动后立即崩溃。
希望有人能帮助我。如果您需要我的更多代码,请告诉我。
提前致谢。
GameViewController.swift
class GameViewController: UIViewController, ADBannerViewDelegate {
var adBannerView = ADBannerView(frame: CGRect.zeroRect)
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "hideAd", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "showAd", object: nil)
self.adBannerView.delegate = self
self.adBannerView.hidden = true
adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
view.addSubview(adBannerView)
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
var scene: SKScene = MainMenu(size: skView.bounds.size)
scene.scaleMode = SKSceneScaleMode.AspectFill
skView.presentScene(scene)
}
}
func handleNotification(notification: NSNotification){
if notification.name == "hideAd"{
adBannerView.hidden = true
}else if notification.name == "showAd"{
adBannerView.hidden = false
}
}
//iAD Setup
func bannerViewWillLoadAd(banner: ADBannerView!) {
println("Ad loads")
}
func bannerViewDidLoadAd(banner: ADBannerView!){
println("Ad loaded")
self.adBannerView.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("resume scene")
let skView = self.view as SKView
skView.paused = false
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
println("pause scene")
let skView = self.view as SKView
skView.paused = true
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Failed to load ad")
self.adBannerView.hidden = true
} more code....
MainMenu.swift
class MainMenu: SKScene{
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(size:CGSize){
super.init(size: size)
NSNotificationCenter.defaultCenter().postNotificationName("showAd", object: nil)
more code...
我用它在子视图中展示 iAd 横幅
var vc = self.view?.window?.rootViewController
vc?.view.addSubview(adView!)
当我想在游戏中隐藏它时(因为iAd降低了FPS)我调用
adView!.removeFromSuperview()
当用户完成游戏后只需添加
var vc = self.view?.window?.rootViewController
vc?.view.addSubview(adView!)
再次显示 iAd 横幅
但您需要在 GameViewController 中实现此代码
class GameViewController: ADBannerViewDelegate(你也需要这个)
也不要忘记在 gameviewcontroller 的顶部导入 iAd
同时将iAd框架导入项目
override func viewDidLoad() {
self.ShowAd()
//这个视图也有didLoad
func ShowAd() {
super.viewDidLoad()
adView = ADBannerView(adType: ADAdType.Banner)
adView!.delegate = self
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.view.addSubview(adView!)
self.view.layoutIfNeeded()
println("Pass")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
adView!.removeFromSuperview()
/*var alert = UIAlertController(title: "iAd Message", message:
"iAd Fail To Load", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: false, completion: nil)
alert.addAction(UIAlertAction(title: "dismiss", style: UIAlertActionStyle.Default,
handler: nil))
self.view.layoutIfNeeded()*/
println("fail")
}
这对我有用
在我的视图控制器中我有这段代码:
// 横幅广告
var SH = UIScreen.mainScreen().bounds.height
let transition = SKTransition.fadeWithDuration(1)
var UIiAd: ADBannerView = ADBannerView()
override func viewWillAppear(animated: Bool) {
var BV = UIiAd.bounds.height
UIiAd.delegate = self
UIiAd.frame = CGRectMake(0, SH + BV, 0, 0)
self.view.addSubview(UIiAd)
}
override func viewWillDisappear(animated: Bool) {
UIiAd.delegate = nil
UIiAd.removeFromSuperview()
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
var BV = UIiAd.bounds.height
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.alpha = 1 // Fade in the animation
UIView.commitAnimations()
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIiAd.alpha = 0
UIView.commitAnimations()
}
func showBannerAd() {
UIiAd.hidden = false
var BV = UIiAd.bounds.height
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.frame = CGRectMake(0, SH - BV, 0, 0) // End position of the animation
UIView.commitAnimations()
}
func hideBannerAd() {
UIiAd.hidden = true
var BV = UIiAd.bounds.height
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.frame = CGRectMake(0, SH + BV, 0, 0) // End position of the animation
UIView.commitAnimations()
}
override func viewDidLoad() {
super.viewDidLoad()
self.UIiAd.hidden = true
self.UIiAd.alpha = 0
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideBannerAd", name: "hideadsID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showBannerAd", name: "showadsID", object: nil)
let scene = GameScene()
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = false
skView.showsNodeCount = false
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFit
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
scene.size = skView.bounds.size
skView.presentScene(scene, transition: transition)
}
为了在我想要的场景中显示它(对我来说是 GameOver 场景)我这样做了:
override func didMoveToView(view: SKView) {
showAds()
// Rest of your code here...
}
func showAds(){
NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil)
}
基本上这段代码会在场景外创建一个横幅,当加载横幅并调用场景时,它会从底部向上滑动。当你切换场景时,它会再次偏移,当你回到那个场景时,它会再次从底部向上滑动。如果横幅未加载,则它不在屏幕上,因此不用担心它会显示白色条。此外,如果它不加载以防万一(使其不可见),它将 alpha 设置为 0。希望这有帮助。
我用 Swift 和 SpriteKit 制作了简单的游戏。一切似乎都正常,但我的 iAd 设置有问题。我只想在特定场景(主菜单和游戏结束)而不是在游戏过程中显示广告横幅。 我的 iAd 设置有效,但前提是它一直显示。 我最后一次修复它的尝试是使用 NSNotificationCenter 方法。我已经按照我在其他 question/answers 此处看到的那样完成了,但应用程序在启动后立即崩溃。 希望有人能帮助我。如果您需要我的更多代码,请告诉我。 提前致谢。
GameViewController.swift
class GameViewController: UIViewController, ADBannerViewDelegate {
var adBannerView = ADBannerView(frame: CGRect.zeroRect)
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "hideAd", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "showAd", object: nil)
self.adBannerView.delegate = self
self.adBannerView.hidden = true
adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
view.addSubview(adBannerView)
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
var scene: SKScene = MainMenu(size: skView.bounds.size)
scene.scaleMode = SKSceneScaleMode.AspectFill
skView.presentScene(scene)
}
}
func handleNotification(notification: NSNotification){
if notification.name == "hideAd"{
adBannerView.hidden = true
}else if notification.name == "showAd"{
adBannerView.hidden = false
}
}
//iAD Setup
func bannerViewWillLoadAd(banner: ADBannerView!) {
println("Ad loads")
}
func bannerViewDidLoadAd(banner: ADBannerView!){
println("Ad loaded")
self.adBannerView.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("resume scene")
let skView = self.view as SKView
skView.paused = false
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
println("pause scene")
let skView = self.view as SKView
skView.paused = true
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Failed to load ad")
self.adBannerView.hidden = true
} more code....
MainMenu.swift
class MainMenu: SKScene{
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(size:CGSize){
super.init(size: size)
NSNotificationCenter.defaultCenter().postNotificationName("showAd", object: nil)
more code...
我用它在子视图中展示 iAd 横幅
var vc = self.view?.window?.rootViewController vc?.view.addSubview(adView!)
当我想在游戏中隐藏它时(因为iAd降低了FPS)我调用
adView!.removeFromSuperview()
当用户完成游戏后只需添加
var vc = self.view?.window?.rootViewController
vc?.view.addSubview(adView!)
再次显示 iAd 横幅
但您需要在 GameViewController 中实现此代码
class GameViewController: ADBannerViewDelegate(你也需要这个) 也不要忘记在 gameviewcontroller 的顶部导入 iAd 同时将iAd框架导入项目
override func viewDidLoad() {
self.ShowAd()
//这个视图也有didLoad
func ShowAd() {
super.viewDidLoad()
adView = ADBannerView(adType: ADAdType.Banner)
adView!.delegate = self
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.view.addSubview(adView!)
self.view.layoutIfNeeded()
println("Pass")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
adView!.removeFromSuperview()
/*var alert = UIAlertController(title: "iAd Message", message:
"iAd Fail To Load", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: false, completion: nil)
alert.addAction(UIAlertAction(title: "dismiss", style: UIAlertActionStyle.Default,
handler: nil))
self.view.layoutIfNeeded()*/
println("fail")
}
这对我有用
在我的视图控制器中我有这段代码:
// 横幅广告
var SH = UIScreen.mainScreen().bounds.height
let transition = SKTransition.fadeWithDuration(1)
var UIiAd: ADBannerView = ADBannerView()
override func viewWillAppear(animated: Bool) {
var BV = UIiAd.bounds.height
UIiAd.delegate = self
UIiAd.frame = CGRectMake(0, SH + BV, 0, 0)
self.view.addSubview(UIiAd)
}
override func viewWillDisappear(animated: Bool) {
UIiAd.delegate = nil
UIiAd.removeFromSuperview()
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
var BV = UIiAd.bounds.height
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.alpha = 1 // Fade in the animation
UIView.commitAnimations()
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIiAd.alpha = 0
UIView.commitAnimations()
}
func showBannerAd() {
UIiAd.hidden = false
var BV = UIiAd.bounds.height
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.frame = CGRectMake(0, SH - BV, 0, 0) // End position of the animation
UIView.commitAnimations()
}
func hideBannerAd() {
UIiAd.hidden = true
var BV = UIiAd.bounds.height
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1) // Time it takes the animation to complete
UIiAd.frame = CGRectMake(0, SH + BV, 0, 0) // End position of the animation
UIView.commitAnimations()
}
override func viewDidLoad() {
super.viewDidLoad()
self.UIiAd.hidden = true
self.UIiAd.alpha = 0
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideBannerAd", name: "hideadsID", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showBannerAd", name: "showadsID", object: nil)
let scene = GameScene()
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = false
skView.showsNodeCount = false
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFit
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
scene.size = skView.bounds.size
skView.presentScene(scene, transition: transition)
}
为了在我想要的场景中显示它(对我来说是 GameOver 场景)我这样做了:
override func didMoveToView(view: SKView) {
showAds()
// Rest of your code here...
}
func showAds(){
NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil)
}
基本上这段代码会在场景外创建一个横幅,当加载横幅并调用场景时,它会从底部向上滑动。当你切换场景时,它会再次偏移,当你回到那个场景时,它会再次从底部向上滑动。如果横幅未加载,则它不在屏幕上,因此不用担心它会显示白色条。此外,如果它不加载以防万一(使其不可见),它将 alpha 设置为 0。希望这有帮助。