gpt4 book ai didi

ios - Spritekit - 如何在特定场景中隐藏 iAd 横幅?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:27:43 24 4
gpt4 key购买 nike

我目前正在从事 Spritekit 项目。

我有 3 个场景:MainMenu、Game、Gameover

我希望仅当用户处于 Game 场景和 Gameover 场景时才显示 iAd。

这是我当前在 ViewController.m 中的 iAd 代码:

- (void) viewWillLayoutSubviews
{

// For iAds
_bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
_bannerView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
_bannerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
_bannerView.delegate = self;
_bannerView.hidden = YES;
[self.view addSubview:_bannerView];
}

#pragma mark - iAds delegate methods
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Occurs when an ad loads successfully
_bannerView.hidden = NO;
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Occurs when an ad fails to load
_bannerView.hidden = YES;
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
// Occurs when the user taps on ad and opens it


return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
// Occurs when the ad finishes full screen
}

问题是,由于 MainMenu 场景是第一个显示的场景,横幅会在成功加载广告后显示在那里。如何让它只在用户处于 Game 场景和 Gameover 场景时出现?

最佳答案

这里最好的方法是使用NSNotificationCenter:

在您的 - (void) viewWillLayoutSubviews
中注册通知

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"hideAd" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];


并在这里处理通知

- (void)handleNotification:(NSNotification *)notification
{
if ([notification.name isEqualToString:@"hideAd"])
{
// hide your banner;
}else if ([notification.name isEqualToString:@"showAd"])
{
// show your banner
}
}


在你的场景中

[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; //Sends message to viewcontroller to show ad.

[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; //Sends message to viewcontroller to hide ad.

谢谢,祝你好运。

关于ios - Spritekit - 如何在特定场景中隐藏 iAd 横幅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25561517/

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