关闭iAD横幅会导致当前场景发生变化

时间:2016-01-16 01:14:22

标签: swift iad

我实施了iAD,它在屏幕底部显示了一个横幅。我在我的showsAD()班级和Menu班级中调用了一个函数GameScene来显示横幅。 GameScene是所有游戏玩法发生的地方。如果我点击iAd Banner中的GameScene,它会因某种原因自动将场景切换回Menu。对于一些在线资源,我使用的代码iAd Banner显示GameViewContorller

class GameViewController: UIViewController, ADBannerViewDelegate {
    var SH = UIScreen.mainScreen().bounds.height
    let transition = SKTransition.fadeWithDuration(1)
    var UIiAd: ADBannerView = ADBannerView()
override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideBannerAd", name: "hideadsID", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "showBannerAd", name: "showadsID", object: nil)

    self.UIiAd.hidden = true
    self.UIiAd.alpha = 0
    }

override func viewWillAppear(animated: Bool) {
    let BV = UIiAd.bounds.height
    UIiAd.delegate = self
    UIiAd.frame = CGRectMake(0, SH + BV, 0, 0)
    self.view.addSubview(UIiAd)
}

override func viewWillDisappear(animated: Bool) {
    UIiAd.delegate = nil
    UIiAd.removeFromSuperview()
}


func bannerViewDidLoadAd(banner: ADBannerView!) {

    _ = UIiAd.bounds.height
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.01) // 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(0.01)
    UIiAd.alpha = 0
    UIView.commitAnimations()
}

func showBannerAd() {
    UIiAd.hidden = false
    let BV = UIiAd.bounds.height

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.01) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH - BV, 0, 0) // End position of the animation
    UIView.commitAnimations()

}

func hideBannerAd() {
    UIiAd.hidden = true
    let BV = UIiAd.bounds.height

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.01) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH + BV, 0, 0) // End position of the animation
    UIView.commitAnimations()
}

然后在GameSceneMenu课程中,这就是我用来展示iAD Banner的内容:

func showAds(){
    NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil)
}

我尝试暂停,但没有奏效。这是代码:

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
        print("Clicked")

        iAdBanner.delegate = self
        // If you want to pause game scene:
        let skView: SKView = self.view as! SKView!
        skView.scene!.paused = true

        return true
    }
    func bannerViewActionDidFinish(banner: ADBannerView!) {
        print("Closed")

        // If you want to continue game scene:
        let skView: SKView = self.view as! SKView
        skView.scene!.paused = false
    }

0 个答案:

没有答案