gpt4 book ai didi

ios - Game Over Class 上的 iAd 横幅实现

转载 作者:行者123 更新时间:2023-12-01 16:27:52 25 4
gpt4 key购买 nike

所以我有一个游戏,游戏结束了类(class)。我在屏幕底部还有一个 iAd 横幅,但它会在整个游戏过程中一直存在。我希望横幅在游戏结束时出现,并在用户按下重新启动游戏按钮时消失。

在我的 RootViewController.mm我有以下代码

- (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];
}

我将如何在游戏结束时只显示 iAd 横幅?

最佳答案

您应该创建一个 Shared iAd Banner在您的 AppDelegate然后出示ADBannerView曾经ViewController你喜欢。此实现考虑到 ADBannerView不会总是收到来自 iAd 网络的广告。 ADBannerViewalpha属性的设置取决于它是否使用 ADBannerView 接收广告。的委托(delegate)方法。这样,您可以只显示和隐藏 ADBannerView当游戏结束时,当你知道 ADBannerView 重置它时只有当它有要展示的广告时才可见。

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;
}

关于ios - Game Over Class 上的 iAd 横幅实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34325011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com