【问题标题】:Keeping iAds in landscape SpriteKit / Swift将 iAd 保持在横向 SpriteKit / Swift
【发布时间】:2015-08-30 00:51:34
【问题描述】:

在我的游戏中,所有场景都是横向的。我将 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 中调用了这个方法

【问题讨论】:

  • 签出stackoverflow.com/questions/26207546/…需要用到UIViewAutoresizingFlexibleWidth
  • 我正在寻找的是如何在点击时将 iAd 保持为横向。全屏时会变成纵向,虽然关闭后又会恢复正常,但加载时看起来并不好。 @肯德尔

标签: ios swift sprite-kit orientation iad


【解决方案1】:

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

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

【讨论】:

  • 所以苹果测试的iAds只支持竖屏? @丹尼尔风暴
  • 当你点击广告时,是的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多