gpt4 book ai didi

ios - 在 Spritekit 中隐藏/显示 iAd

转载 作者:可可西里 更新时间:2023-11-01 03:26:13 26 4
gpt4 key购买 nike

我一直在尝试弄清楚如何在我的 Spritekit 场景中隐藏和显示 iAd。目前我有这样的设置:

ViewController.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>
#import <iAd/iAD.h>


@interface ViewController : UIViewController <ADBannerViewDelegate> {

ADBannerView *adView;

}

-(void)showsBanner;
-(void)hidesBanner;



@end

ViewController.m

#import "ViewController.h"
#import <UIKit/UIKit.h>
#import <iAd/iAD.h>
#import "MyScene.h"

#import <SpriteKit/SpriteKit.h>


@implementation ViewController

- (void)viewDidLoad
{

[super viewDidLoad];

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

}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (!self.bannerIsVisible) {
[UIView beginAnimations:@"animatedAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 0.0);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (!self.bannerIsVisible) {
[UIView beginAnimations:@"animatedAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 0.0);
[adView setAlpha:0];
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}

-(void)hidesBanner {

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


-(void)showsBanner {

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

}


etc...


@end

然后在我的场景中,我用指针捕获我的 View Controller :

ViewController *controller;


controller = [[ViewController alloc] init];
[controller hidesBanner];

我的 nslog 在控制台中运行,所以我知道它正在运行。但横幅不会隐藏。有什么想法吗?我对 Objective C 很陌生,所以我觉得我只是在做一些愚蠢的事情。

最佳答案

正如 Huygamer 所说,您正在创建一个 View Controller 的新实例,因此当您调用您的方法时 [controller hidesBanner]; 您指的是另一个对象。

这里最好的方法是使用NSNotificationCenter:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

并在您想要隐藏或显示广告时向您的 View Controller 发送消息:

ViewController.m

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

关于ios - 在 Spritekit 中隐藏/显示 iAd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21664295/

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