Swift SpriteKit iAd采用横向模式

时间:2016-03-10 13:13:52

标签: ios swift layout sprite-kit iad

我有一个Spritekit游戏,我可以显示iAds。我的问题是,当我在横向模式下打开手机时,iAd横幅不会拉伸到达屏幕边界宽度。这是代码:

   import SpriteKit
   import UIKit
 import iAd

class GameScene: SKScene, SKPhysicsContactDelegate, ADBannerViewDelegate {

var adBannerView:ADBannerView = ADBannerView()

    override func didMoveToView(view: SKView) {
       loadAds()
       self.adBannerView.hidden = true
       ....



 func loadAds()->ADBannerView{
            //adBannerView = ADBannerView(frame: CGRect.zero)


    adBannerView.center = CGPoint(x: view!.frame.size.width / 2, y: view!.frame.size.height - 550) 
    adBannerView.delegate = self

    self.view?.addSubview(adBannerView)



    return adBannerView



   }
     func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
   // banner.alpha = 0.0
    //self.adBannerView.alpha = 0.0
    print("Ad cannot load")
    banner.alpha = 0.0
    self.adBannerView.alpha = 0.0
    self.adBannerView.hidden = true
    //banner.alpha = 0.0
    //self.adBannerView.alpha = 0.0
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
    print("ad did load.")
    self.adBannerView.hidden = false
}

为什么在横向模式下,我的iAd横幅无法自动拉伸以达到屏幕尺寸宽度?

3 个答案:

答案 0 :(得分:1)

尝试覆盖场景的didChangeSize方法:

override func didChangeSize(oldSize: CGSize) {
    super.didChangeSize(oldSize)
    // your resizing code here
} 

您还需要覆盖GameViewController中的其他方法:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let skView = self.view as! SKView
    if let scene = skView.scene {

        let scene = scene as! GameScene
        if scene.size != self.view.bounds.size {
            scene.size = self.view.bounds.size
        }
    }
}

当设备旋转时,会调用viewDidLayoutSubviews方法,您可以在此处调整场景大小并调用游戏逻辑。

答案 1 :(得分:1)

我在GitHub上有一个帮手,你可以看看。 https://github.com/crashoverride777/Swift2-iAds-AdMob-CustomAds-Helper

基本上我所做的是在viewController

中更改此方法中的广告
   override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    coordinator.animateAlongsideTransition({ (UIViewControllerTransitionCoordinatorContext) -> Void in



       let orientation = UIApplication.sharedApplication().statusBarOrientation
      switch orientation {
       case .Portrait:
         print("Portrait")
          // Do something
       default:
         print("Anything But Portrait")
           // Do something else
       }

        }, completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
            print("Device rotation completed")
    })
}

这会在屏幕旋转时调用,我会用它来更新我的广告尺寸。

这有帮助吗?

答案 2 :(得分:0)

我尝试使用Display an iAd banner in landscape in swift & spritekit

中的代码
let screenBounds: CGRect = UIScreen.mainScreen().bounds

    var adBannerView: ADBannerView  
    adBannerView = ADBannerView(frame: CGRectMake(0, 0, 50, screenBounds.width))
    adBannerView.center = CGPoint(x: screenBounds.width/2, y: screenBounds.height-adBannerView.frame.height)
    adBannerView.delegate = self
    adBannerView.hidden = true
    view.addSubview(adBannerView)

iAd在模拟器中完美地在横向模式下工作,但在我的iPhone上给出同样的问题......为什么?