如何在不同的SpriteKit场景中显示iAd横幅

5
我使用Swift和SpriteKit制作了一个简单的游戏。所有东西似乎都正常工作,但我在我的iAd设置中遇到了问题。我只想在特定场景(主菜单和游戏结束)中显示广告横幅,而不是在游戏过程中都显示。
我的iAd设置能够正常工作,但只有在一直显示的情况下才有效。
我最后一次尝试修复它时使用了NSNotificationCenter方法。我已按照其他问题/答案中看到的方式完成了此操作,但应用程序在启动后立即崩溃。
希望有人能帮助我。如果您需要更多我的代码,请告诉我。
提前致谢。
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...

可能是重复的问题:在使用Sprite Kit时如何启用iAd? - i_am_jorf
https://dev59.com/y3zaa4cB1Zd3GeqPOEud - i_am_jorf
https://dev59.com/FHzaa4cB1Zd3GeqPRpEj - i_am_jorf
http://stackoverflow.com/questions/26418114/iads-with-swift-in-spritekit - i_am_jorf
2个回答

8

这对我有用

在我的视图控制器中,我有以下代码:

// 横幅广告

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)
}

基本上,这段代码创建了一个横幅,并且当这个横幅被加载并且场景被调用时,它从底部滑动。当你切换场景时,它会重新偏移位置,当你回到那个场景时,它再次从底部滑上来。如果横幅没有加载,它就会在屏幕外,所以不必担心会显示白色条。此外,如果横幅没有加载,它会将透明度设置为0(使其不可见)。希望这可以帮到你。


1

我使用这段代码将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()

//在视图加载中也要考虑这个

     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")
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接