代码之家  ›  专栏  ›  技术社区  ›  Sam Acquaviva

将iAds保存在景观SpriteKit/Swift中

  •  3
  • Sam Acquaviva  · 技术社区  · 9 年前

    在我的游戏中,所有场景都是风景。我在屏幕底部实现了处于横向模式的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 回复  |  直到 9 年前
        1
  •  2
  •   Daniel Storm    9 年前

    这个问题主要只适用于iAd在开发期间发布的测试广告。应用程序在App Store上运行后,应用程序中出现的大多数实时横幅广告将在用户点击广告时同时支持横向和纵向布局。

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