使用SpriteKit隐藏iOS中的Admob横幅

时间:2015-04-28 08:38:45

标签: ios sprite-kit admob

我在Xcode中使用了一个名为Sprite kit的2D游戏引擎,我希望在特定区域(如游戏场景)中隐藏我的广告横幅,然后在玩家游戏结束后显示它。但是我在尝试访问其他场景/类中的横幅的隐藏属性时遇到了麻烦。

GameViewController.h

#import <UIKit/UIKit.h>

#import <SpriteKit/SpriteKit.h>

#import <GoogleMobileAds/GoogleMobileAds.h>

#import <AVFoundation/AVFoundation.h>

@interface GameViewController : UIViewController

-(void) hideBanner;

@end

GameViewController.m

@implementation GameViewController

-(void) hideBanner {
    self.bannerView.hidden = YES;   
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Create a banner ad and add it to the view hierarchy.

    self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];

    //TEST UNIT ID

    self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
    self.bannerView.rootViewController = self;

    [self.view addSubview:self.bannerView];

    GADRequest *request = [GADRequest request];

    request.testDevices = @[ @"*log id*" ];

    [self.bannerView loadRequest:request];
}

GameScene.h

@class GameViewController;

@interface GameScene : SKScene <SKPhysicsContactDelegate>

@property (strong, nonatomic) GameViewController *gameViewController;

@end

GameScene.m

//This line of code will be executed in the "performGameOver" method but it does not work and the banner is still shown? 
    [self.gameViewController hideBanner];

1 个答案:

答案 0 :(得分:0)

您应该使用NSNotification

在viewController.m中

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

-(void)hidesBanner {

    NSLog(@"HIDING BANNER");
    [adView setAlpha:0];
    self.bannerIsVisible = NO;
}

-(void)showsBanner {

    NSLog(@"SHOWING BANNER");
    [adView setAlpha:1];
    self.bannerIsVisible = YES;
}

在你的场景中:

向viewcontroller发送消息以显示广告。

[[NSNotificationCenter defaultCenter] postNotificationName:@"showAd" object:nil]; 

向viewcontroller发送消息以隐藏广告。

[[NSNotificationCenter defaultCenter] postNotificationName:@"hideAd" object:nil]; 

更多信息:

  

https://stackoverflow.com/a/21967530/4078517