2014-02-27 58 views
3

我已經加入網絡成癮者到我的雪碧套件遊戲下面的代碼:隱藏場景中的iAd雪碧套件

在viewController.h文件

@property (strong, nonatomic) IBOutlet ADBannerView * adBannerView; 

在viewController.m文件

- (void)viewWillLayoutSubviews 
{ 
    [super viewWillLayoutSubviews]; 

    // Configure the view. 
    SKView * skView = (SKView *)self.view; 
    if (!skView.scene) { 

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

     _adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero]; 
     _adBannerView.delegate = self; 
     [_adBannerView setFrame:CGRectMake(0, 0, 460, 320)]; 
     [self.view addSubview:_adBannerView]; 

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

這顯示了每個場景中的iAd。有沒有辦法在一些場景中隱藏iAd?

蘋果的iAd編程指南說:

只有當你打算把它顯示給用戶創造一個橫幅視圖。否則,它可能會在廣告中循環並耗盡應用程序的可用廣告列表。

這是所有可能的場景嗎?

回答

0

最乾淨的解決方案是申報和實施協議,讓UIViewController從現場知道它應該隱藏廣告。

@protocol MySceneDelegate <NSObject> 
- (void)hideAd; 
@end 

@interface MyScene : SKScene 
@property (weak) id <MySceneDelegate> delegate; 
@end 

視圖控制器,顯示現場應該實現一個hideAd方法,並設置本身作爲場景的代表。例如:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

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

    // Create and configure the scene. 
    MyScene * scene = [MyScene sceneWithSize:skView.bounds.size]; 
    scene.scaleMode = SKSceneScaleModeAspectFill; 

    // Set the delegate 
    [scene setDelegate:self]; 

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

然後在現場,你可以調用其設定爲一個委託視圖控制器的hideAd方法:

if ([_delegate respondsToSelector:@selector(closeScene)]) 
{ 
    [_delegate performSelector:@selector(hideAd)]; 
} 

,並刪除大旗hideAd方法。

要隱藏廣告視圖,您應該:

調整您的橫幅視圖的框架將屏幕之外重新調整內容大小 視圖的框架來覆蓋空間中,原本主辦的旗幟

希望它可以幫助。

4

是的,有一種方法可以在某些場景中隱藏iAd。

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    //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]; 

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

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

    // Present the scene. 
    [skView presentScene:scene]; 
    self.canDisplayBannerAds = YES; 

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero]; 
    adView.frame = CGRectOffset(adView.frame, 0, 0.0f); 
    adView.delegate=self; 
    [self.view addSubview:adView]; 

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

,並在其中要隱藏的旗幟場景......從答案

[[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. 
+2

哇,好複製粘貼.. http://stackoverflow.com/a/21967530/1898107 – bangerang