如何在不同的 SpriteKit 场景中显示 iAd 横幅
Posted
技术标签:
【中文标题】如何在不同的 SpriteKit 场景中显示 iAd 横幅【英文标题】:How to display iAd banner in different SpriteKit Scenes 【发布时间】:2015-01-06 22:03:54 【问题描述】:我使用 Swift 和 SpriteKit 制作了简单的游戏。一切似乎都正常,但我的 iAd 设置有问题。我只想在特定场景(主菜单和游戏结束)中显示广告横幅,而不是在游戏过程中。 我的 iAd 设置有效,但前提是它始终显示。 我最后一次尝试修复它是使用 NSNotificationCenter 方法。我已经这样做了,因为我在此处的其他问题/答案中看到了它,但应用程序在启动后立即崩溃。 希望有人可以帮助我。如果您需要更多我的代码,请告诉我。 提前致谢。
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...
【问题讨论】:
How can I enable iAd when using Sprite Kit? 的可能重复项 ***.com/questions/21664295/… ***.com/questions/21816690/… ***.com/questions/26418114/… 【参考方案1】:这对我有用
在我的视图控制器中,我有以下代码:
// 横幅广告
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。希望这会有所帮助。
【讨论】:
【参考方案2】:我用它在子视图中展示 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")
【讨论】:
以上是关于如何在不同的 SpriteKit 场景中显示 iAd 横幅的主要内容,如果未能解决你的问题,请参考以下文章
SpriteKit:你如何突出场景的一部分,就像在教程中一样?
在不同的 UIKit 和 SpriteKit 场景中连续播放音频