将iAds保存在景观SpriteKit / Swift中

时间:2015-08-30 00:51:34

标签: ios swift sprite-kit orientation iad

在我的游戏中,所有场景都在风景中。我在屏幕底部实现了横向模式的iAd横幅。但是,当我点击它并加载它时,它会将其方向更改为纵向。退出广告后,游戏恢复正常。是否可以在加载时将广告保持在横向模式?以下是我用于创建iAd的代码。

import UIKit
import SpriteKit
import iAd

class GameViewController: UIViewController, ADBannerViewDelegate {

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) */

    UIiAd.setTranslatesAutoresizingMaskIntoConstraints(false)
    UIiAd.delegate = self
    self.view.addSubview(UIiAd)
    let viewsDictionary = ["bannerView":UIiAd]
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[bannerView]|", options: .allZeros, metrics: nil, views: viewsDictionary))
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerView]|", options: .allZeros, metrics: nil, views: viewsDictionary))

}

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(10) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH - BV, 2048, 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 = MainMenu(size: CGSize(width: 2048, height: 1356))
    let skView = self.view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true
    skView.ignoresSiblingOrder = true
    scene.scaleMode = .AspectFill
    skView.presentScene(scene)
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    println("Clicked")
    return true
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

}

我在GameScene中调用此方法

1 个答案:

答案 0 :(得分:2)

此问题仅适用于iAd在开发过程中投放的测试广告。一旦您的应用程序在App Store上运行,大多数将在您的应用程序中显示的实时横幅广告将在用户点击广告时支持横向和纵向布局。

当广告构建为仅以纵向显示时,无法强制广告重新布局横向广告,反之亦然。