ios - 单击 iAd 时运行功能(暂停菜单) SpriteKit Swift

标签 ios swift sprite-kit iad

我在我的 SpriteKit/Swift 游戏中实现了 iAds。当用户点击广告时,我无法找到如何在 GameScene 中运行功能(我有一个可以调出暂停菜单的功能),而不仅仅是暂停场景。我该如何做到这一点?谢谢。

编辑:这是我的 GameViewController。

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)
}

override func prefersStatusBarHidden() -> Bool {
    return true
}   

然后在GameScene中我导入iAd,添加ADBannerViewDelegate,然后添加这段代码

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

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    iAdBanner.delegate = self
    println("Clicked")
    paused = true
    return true
}

最佳答案

为横幅 iAd 尝试此功能:

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

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

    return true
}

还有:

func bannerViewActionDidFinish(banner: ADBannerView!) {
    println("Closed")

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

关于ios - 单击 iAd 时运行功能(暂停菜单) SpriteKit Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32281572/

相关文章:

ios - 使 Sprite 出现在随机的 y 点

ios - 如何将粒子附加到移动的物体上

swift - 如何在 SKScene 中的某些 SKSpriteNode 上制作朦胧/多云效果

ios - 如何重命名 Storyboard并保留其他 Storyboard的链接

ios - 具有相同根 bundle 的两个应用程序的 SSL 证书(推送通知)

ios - Swift Swipe 手势识别器使程序崩溃

swift - Sprite 出现在节点数中但不可见

ios - 在 XCTest (UITesting) 中获取设备名称

ios - 生成随机数并在添加/减去它们后比较 objective-c 中的结果

objective-c - 我如何为 IOS 简化这个 Objective-C 代码?