ios - 将 ADBannerView 添加到 SKScene

标签 ios objective-c sprite-kit iad

我正在尝试将 iAd 横幅添加到我的游戏结束场景,但我不知道如何将 UIView 添加到 SKScene。

应用 View Controller 是:

- (void)viewWillLayoutSubviews
    {
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [GameOverScene sceneWithSize:skView.bounds.size andScore:0];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}

和 GameOverScene:
#import "GameOverScene.h"
#import <iAd/iAd.h>
@implementation GameOverScene

+(id)sceneWithSize:(CGSize)size andScore:(NSInteger)score{
     return [[self alloc] initWithSize:size andScore:score];
}
-(id)initWithSize:(CGSize)size andScore:(NSInteger)score{
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        //some stuff here...

        ADBannerView* banner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)];
        [self.scene.view addSubview:banner];

        //[self.view addSubview:banner];

    }
    return self;
}
@end

有什么想法或建议吗?谢谢!

最佳答案

使用 NSNotificationCenter。为您简化:

1)在SKView里面的storyboard中添加一个AdBannerView。 (或者您可以使用 ViewController.m 中的代码添加)

2)在你的 ViewController.h 中定义 ADBannerViewDelegate 并添加这个(记得在 Storyboard中链接导出)

@property (weak, nonatomic) IBOutlet ADBannerView *banner;

3)在你的 ViewController.m
- (void)viewDidLoad
{
   self.banner.hidden = YES;
   //Add view controller as observer
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"hideAd" object:nil];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];
}

- (void)showBanner
{
    self.banner.hidden = NO;
}

- (void)hidesBanner
{
    self.banner.hidden = YES;
}

//Handle Notification
- (void)handleNotification:(NSNotification *)notification
{
    if ([notification.name isEqualToString:@"hideAd"]) {
        [self hidesBanner];
    }else if ([notification.name isEqualToString:@"showAd"]) {
        [self showBanner];
    }
}

4) 在您的 Scene.m 中,为了显示广告

例如玩家死了,你想显示横幅
// Show banner when iad loads
[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; //Sends message to viewcontroller to show ad.

5) 在你的 Scene.m 中,为了隐藏广告

例如玩家重新开始游戏
// Show banner when iad loads
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; //Sends message to viewcontroller to show ad.

*建议在未加载时隐藏广告。

关于ios - 将 ADBannerView 添加到 SKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24736082/

相关文章:

ios - 从核心数据模型 Swift 3 中删除一行

ios - SwiftUI Xcode 12.3 无法更改工具栏中的按钮大小

ios - 在 didUpdateFrame 方法中保存 ARSCNView 快照会导致相机卡住

objective-c - AnimationForKey 上的 EXC_BAD_ACCESS :

swift - 当 2 个手指在屏幕上时应用程序崩溃,触摸开始,触摸移动

ios - 如何通过拖动旋转 Sprite

ios - 如何使 UITableView 适合内容的大小

objective-c - 在 Objective C 中声明变量时什么时候不应该使用星号 (*)

objective-c - 音乐未与AVAudioPlayer循环播放

ios - 核心数据父实体生成的NSManagedObject子类