gpt4 book ai didi

ios - 仅在特定场景中显示 adMob 横幅; swift

转载 作者:行者123 更新时间:2023-11-28 10:13:16 24 4
gpt4 key购买 nike

在我的游戏中,我有 4 个不同的场景。现在我想在这 4 个场景中的 2 个场景中转换一些 AdMob 横幅广告。

我是这样试过的。

class GameViewController: UIViewController, GADBannerViewDelegate {


var bannerView: GADBannerView!



override func viewDidLoad() {

super.viewDidLoad()


if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'

if var scene = StartScene(fileNamed: "StartScene") {





// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill

// Present the scene
view.presentScene(scene)



}



if var scene = MainMenuScene(fileNamed: "MainMenuScene") {



bannerView = GADBannerView(adSize: kGADAdSizeBanner)

self.view.addSubview(bannerView)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-2567181742111069/9977523039"
bannerView.load(request)
bannerView.center = CGPoint(x: 329, y: 350)

}


view.ignoresSiblingOrder = true

view.showsPhysics = false


view.showsFPS = false
view.showsNodeCount = false



}



}

问题是,广告仍然显示在每个场景中。

有人知道如何解决这个问题吗?

最佳答案

广告显示在每个场景中,因为横幅 View 附加到 GameViewController(充当 super View ),它包含子场景。

viewDidLoad() 中创建并加载横幅广告,并在您不希望它出现的场景中隐藏它,然后在您想要的场景中显示它:

class GameViewController: UIViewController, GADBannerViewDelegate {


var bannerView: GADBannerView!



override func viewDidLoad() {

super.viewDidLoad()

bannerView = GADBannerView(adSize: kGADAdSizeBanner)

self.view.addSubview(bannerView)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]
bannerView.rootViewController = self
bannerView.delegate = self
bannerView.adUnitID = "ca-app-pub-2567181742111069/9977523039"
bannerView.load(request)
bannerView.center = CGPoint(x: 329, y: 350)


if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'

if var scene = StartScene(fileNamed: "StartScene") {

// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill

// Present the scene
self.bannerView.isHidden = true
view.presentScene(scene)
}



if var scene = MainMenuScene(fileNamed: "MainMenuScene") {

self.bannerView.isHidden = false

}


view.ignoresSiblingOrder = true

view.showsPhysics = false


view.showsFPS = false
view.showsNodeCount = false



}

}

您还可以使用 addSubViewremoveFromSuperview 函数在 superview(GameViewController) 及其窗口中添加/删除

关于ios - 仅在特定场景中显示 adMob 横幅; swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45165886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com