Spritekit支持iAd

时间:2016-04-14 22:12:11

标签: swift sprite-kit iad

我一直在网上寻找关于如何在我的spritekit游戏中包含iad横幅的教程。例如,我看了这个:Swift SpriteKit iAd但是我得到了一些错误,因为新的swift 2和swift 1可能会出错。但是你能解释一下如何在我的spritekit游戏中包含IAD。先感谢您。

1 个答案:

答案 0 :(得分:0)

确保您的GameViewController中包含此代码。我刚测试了它,它适用于横向和纵向模式。

 class GameViewController: UIViewController, ADBannerViewDelegate {

//--------------------
//-----iAd Banner-----
//--------------------
var SH = UIScreen.mainScreen().bounds.height
let transition = SKTransition.fadeWithDuration(0)
var UIiAd: ADBannerView = ADBannerView()

override func viewWillAppear(animated: Bool) {
    let 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!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.5) // 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(0.5)
    UIiAd.alpha = 0
    UIView.commitAnimations()
}

func showBannerAd() {
    UIiAd.hidden = false
    let BV = UIiAd.bounds.height

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH - BV, UIScreen.mainScreen().bounds.width, 0)
    UIView.commitAnimations()
}

func hideBannerAd() {
    UIiAd.hidden = true
    let BV = UIiAd.bounds.height

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH + BV, UIScreen.mainScreen().bounds.width, 0) //Beginning position of the ad
    UIView.commitAnimations()
}



//--------------------
//-----View Loads-----
//--------------------
override func viewDidLoad() {
    super.viewDidLoad()
    //iAd Control
    self.UIiAd.hidden = true
    self.UIiAd.alpha = 0
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.hideBannerAd), name: "hideadsID", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.showBannerAd), name: "showadsID", object: nil)
}

在类声明之外添加以下内容。你去哪个班级并不重要,只要它是全球的,所以你可以从任何游戏场景中访问它们。

 //-------------
 //-----Ads-----
 //-------------
 func showAds() {
     NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil)
 }
 func hideAds() {
     NSNotificationCenter.defaultCenter().postNotificationName("hideadsID", object: nil)
 }

然后,只要您想在特定场景中展示广告或在任何时候,只需拨打" showAds()"功能或" hideAds()"隐藏它们。