2015-12-17 2 views
2

J'ai donc un jeu qui a un jeu en cours. J'ai également une bannière iAd en bas de l'écran, mais elle reste là pendant toute la durée du jeu. Je veux que la bannière apparaisse lorsque le jeu se termine, et disparaisse lorsque l'utilisateur appuie sur le bouton de redémarrage du jeu.Mise en œuvre de la bannière iAd sur Game Over Class

Dans mon RootViewController.mm j'ai le code suivant

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

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

Comment pourrais-je aller sur l'affichage d'une seule bannière iAd la fin du jeu?

+3

S'il vous plaît ne pas vandaliser vos messages. Si vous souhaitez le supprimer, signalez l'attention du modérateur à l'aide d'un indicateur de modérateur personnalisé et expliquez pourquoi vous pensez qu'il doit être supprimé. –

Répondre

0

Vous pouvez essayer ce morceau de code:

Lorsque votre jeu se termine: [self showiAdBanner];

Lorsque votre jeu commence: [self hideiAdBanner];

Si vous utilisez ce code, vous aurez envie de le remplacer:

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

avec ceci:

[self showiAdBanner]; 

Deux méthodes pour montrer IAD et se cachent iAd:

- (void)showiAdBanner { 
    if(!_adView) { // only add to view if it's not already there 
     _adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 320, 50)]; //initialize it 
     _adView.delegate = self; // set delegate 
    } 
    _adView.hidden = NO; //reveal it 
    _bannerIsVisible = YES; //set bool to yes 
} 

- (void)hideiAdBanner { 
    _adView.hidden = YES; //hide it 
    _bannerIsVisible = NO; // set bool to no 
} 

Rappelez-vous d'ajouter cette partie ainsi:

@interface RootViewController() <ADBannerViewDelegate> 
@property (nonatomic, strong) ADBannerView *adView; 
@property (nonatomic) BOOL bannerIsVisible; 
@end 

Si vous voulez obtenir la fantaisie, au lieu de le cacher, vous pouvez animer/disparaître.

+0

Im obtenir un tas d'identifiants non déclarés – johnooseeee

+0

ah que dit-il? –

+0

l'utilisation de l'identificateur non déclaré "_adView", "_bannerIsVisible", etc. – johnooseeee

0

Vous devez créer un Shared iAd Banner dans votre AppDelegate puis présenter le ADBannerView sur lequel jamais ViewController vous aimez. Cette implémentation prend en compte le fait qu'un ADBannerView ne recevra pas toujours une annonce du réseau iAd. La propriété de ADBannerView est définie selon qu'elle reçoit ou non une annonce à l'aide des méthodes de délégation ADBannerView. De cette façon, vous pouvez simplement afficher et masquer le ADBannerView quand le jeu se termine et quand vous le réinitialisez sachant que le ADBannerView ne sera visible que s'il a une annonce à présenter.

AppDelegate.h

#import <UIKit/UIKit.h> 
@import iAd; // Import iAd 

@interface AppDelegate : UIResponder <UIApplicationDelegate, ADBannerViewDelegate> // Include delegate 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) ADBannerView *adView; 

AppDelegate.m

#import "AppDelegate.h" 

@interface AppDelegate() 

@end 

@implementation AppDelegate 

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Create our one ADBannerView 
    _adView = [[ADBannerView alloc]init]; 
    // Set delegate and hide banner initially 
    _adView.delegate = self; 
    _adView.hidden = YES; 
    return YES; 
} 

// iAd delegate methods 
-(void)bannerViewDidLoadAd:(ADBannerView *)banner { 
    NSLog(@"bannerViewDidLoadAd"); 
    _adView.alpha = 1.0; 
} 

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { 
    NSLog(@"didFailToReceiveAdWithError: %@",error); 
    _adView.alpha = 0.0; 
} 

ViewController.m

#import "ViewController.h" 
#import "AppDelegate.h" 

@interface ViewController() { 
    AppDelegate *appDelegate; 
} 

@end 

@implementation ViewController 

-(void)viewDidLoad { 
    [super viewDidLoad]; 

    // Create reference to our app delegate 
    appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    // Position 
    appDelegate.adView.center = CGPointMake(self.view.center.x, 
              self.view.frame.size.height - appDelegate.adView.frame.size.height/2); 
    // Add to view 
    [self.view addSubview:appDelegate.adView]; 

    // I'm just calling the gameOver function for example 
    // You should add the contents of the function to something more suitable 
    [self gameOver]; 
} 

-(void)gameOver { 
    // Unhide the ADBannerView in which ever function is called first in the gameover view 
    // I'm guessing you have a UIButton that this would work in 
    appDelegate.adView.hidden = NO; 
} 

-(void)gameReset { 
    // When you reset the game hide the ADBannerView 
    // I'm guessing you have a UIButton that this would work in also 
    appDelegate.adView.hidden = YES; 
}