保持 iAds 横向 SpriteKit / Swift

Keeping iAds in landscape SpriteKit / Swift

在我的游戏中,所有场景都是横向的。我将 iAd 横幅实现为在屏幕底部处于横向模式。但是,当我单击它并加载时,它会将其方向更改为纵向。当我退出广告后,游戏returns就正常了。是否可以在加载时将广告保持在横向模式?下面是我用于创建 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中调用了这个方法

该问题主要只存在于iAd在开发过程中投放的测试广告。当您的应用程序在 App Store 上线后,将出现在您的应用程序中的大多数实时横幅广告将在用户点击广告时支持横向和纵向布局。

无法强制将广告重新布局为仅纵向显示,反之亦然。