将ADBannerView添加到SKScene [英] Add ADBannerView to a SKScene

查看:113
本文介绍了将ADBannerView添加到SKScene的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to add an iAd banner to my game over scene, but I don't know how to add UIView to an SKScene.

应用视图控制器是:

- (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:

and the 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

任何想法或建议?谢谢!

Any idea or suggestion? Thanks!

推荐答案

使用NSNotificationCenter。为了方便您:

Use NSNotificationCenter. To make it simple for you:

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

1) Add a AdBannerView in the storyboard inside the SKView. (Or you can add in using code in your ViewController.m)

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

2) In your ViewController.h define ADBannerViewDelegate and add in this (remember to link the outlet in storyboard)

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

3)在你的ViewController.m中

3) In your 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中,为了显示广告

4) In your Scene.m, in order to display the ad

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

e.g. player died and you want to show the banner

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

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

5) In your Scene.m, in order to hide the ad

例如玩家重新启动游戏

// Show banner when iad loads
[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; //Sends message to viewcontroller to show ad.

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

*Advise hide the ad when it's not loaded.

这篇关于将ADBannerView添加到SKScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆