iAds 横幅 jerking/changing 场景大小,每次加载 iAd 横幅时。

iAds banner jerking/changing size of scene, every time iAd banner is loaded.

如果 iAds 横幅不改变场景,我无法将 iAds 实施到游戏中,例如每次加载广告时都会抖动屏幕。请帮助解决这个问题。

class GameViewController: UIViewController,ADBannerViewDelegate{
@IBOutlet var adBannerView: ADBannerView? //connect in IB connection inspector with your ADBannerView

override func viewDidLoad() {
    super.viewDidLoad()

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = true
        skView.showsNodeCount = true

        var defaultsV = NSUserDefaults.standardUserDefaults();
        defaultsV.setInteger(0, forKey: "gameOverCount")
        defaultsV.setBool(false, forKey: "firstSessionEnded")
        defaultsV.synchronize()

        //self.adBannerView!.frame = CGRectMake(0, self.view.frame.size.height-self.adBannerView!.frame.size.height, self.adBannerView!.frame.size.width, self.adBannerView!.frame.size.height)




        self.adBannerView!.delegate = self
        self.adBannerView!.hidden = true //hide until ad loaded

        self.canDisplayBannerAds = true
        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true
        self.adBannerView?.sizeToFit()

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFill
        skView.presentScene(scene)



    }
}

func bannerViewWillLoadAd(banner: ADBannerView!) {
    NSLog("bannerViewWillLoadAd")
    //self.canDisplayBannerAds = true
    self.adBannerView!.hidden = true //hide until ad loaded

    self.adBannerView!.frame = CGRectMake(0, self.view.frame.size.height-self.adBannerView!.frame.size.height, self.adBannerView!.frame.size.width, self.adBannerView!.frame.size.height)



}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    NSLog("bannerViewDidLoadAd")
    self.adBannerView!.frame = CGRectMake(0, self.view.frame.size.height-self.adBannerView!.frame.size.height, self.adBannerView!.frame.size.width, self.adBannerView!.frame.size.height)

    self.adBannerView!.hidden = false //now show banner as ad is loaded
    //self.canDisplayBannerAds = true


}

func bannerViewActionDidFinish(banner: ADBannerView!) {
    NSLog("bannerViewDidLoadAd")


    //optional resume paused game code

}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    NSLog("bannerViewActionShouldBegin")

    //optional pause game code

    return true
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    NSLog("bannerView")

   self.adBannerView!.hidden = true
}

使用iads横幅的方法:

override func viewWillAppear(animated: Bool) {
// View is about to be obscured by an advert. 
// Pause activities if necessary
}

override func viewWillDisappear(animated: Bool) {
// Advert has been dismissed. Resume paused activities
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError 
                    error: NSError!) {
    banner.removeFromSuperview()
    self.view.layoutIfNeeded()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.canDisplayBannerAds = true

    rectangleAdView = ADBannerView(adType: ADAdType.MediumRectangle)
    rectangleAdView?.delegate = self
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.view.addSubview(banner)
    self.view.layoutIfNeeded()
 }

 func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    banner.removeFromSuperview()
    self.view.layoutIfNeeded()
}

http://www.techotopia.com/index.php/Integrating_iAds_into_an_iOS_8_App_using_Swift

您建议您始终将 canDisplayBannerAds 设置为 false,因为这允许 viewController 在显示广告时调整视图大小。如果您需要创建另一个布尔值来确定广告的状态。

//ViewControlller.m
self.canDisplayBannerAds = false