首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向SKScene添加ADBannerView

向SKScene添加ADBannerView
EN

Stack Overflow用户
提问于 2014-07-14 19:59:23
回答 2查看 636关注 0票数 1

我正在尝试添加一个iAd横幅到我的游戏over scene,但我不知道如何将UIView添加到SKScene中。

应用程序视图控制器是:

代码语言:javascript
复制
- (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:

代码语言:javascript
复制
#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

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

EN

回答 2

Stack Overflow用户

发布于 2014-07-17 11:27:05

使用NSNotificationCenter。让它对您来说更简单:

1)在SKView内部的故事板中添加一个AdBannerView。(或者您可以使用ViewController.m中的代码添加)

2)在你的ViewController.h中定义ADBannerViewDelegate并添加这个(记住在故事板中链接插座)

代码语言:javascript
复制
@property (weak, nonatomic) IBOutlet ADBannerView *banner;

3)在您的ViewController.m

代码语言:javascript
复制
- (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中,为了显示广告

例如,玩家死了,而您想要显示横幅

代码语言:javascript
复制
// Show banner when iad loads
[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; //Sends message to viewcontroller to show ad.

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

玩家重新开始游戏

代码语言:javascript
复制
// Show banner when iad loads
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; //Sends message to viewcontroller to show ad.

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

票数 2
EN

Stack Overflow用户

发布于 2014-07-15 00:19:28

滚动到小时4: iAd。这是一个很好的教程,它可以帮助你90%,最后10%你可以尝试自己

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24736082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档