我一直在试图弄清楚如何在我的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
然后在我的场景中,我用指针抓住我的viewcontroller:
ViewController *controller;
controller = [[ViewController alloc] init];
[controller hidesBanner];
我的nslog在控制台中运行,所以我知道它正在经历。但横幅不会隐藏。有什么想法吗?我对目标c很新,所以我有一种感觉,我只是在做一些愚蠢的事情。
答案 0 :(得分:18)
就像Huygamer所说,你正在创建一个视图控制器的新实例,所以当你调用方法[controller hidesBanner];
时,你指的是另一个对象。
此处最好的方法是使用NSNotificationCenter
:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html
每当您想要隐藏或展示广告时,都会向您的viewcontroller发送消息:
<强> 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.
答案 1 :(得分:0)
当然,有2个对象以及为什么你认为它可以做到?
如果您想访问skscene的父级,请执行此操作
UIViewController * vc = self.view.window.rootViewController;
您可以访问此skscene的父级,您可以在此场景的父级处执行hideBanner。简单?
答案 2 :(得分:0)
以下是我使用SpriteKit场景(iPhone 6上的Xcode 6.1和iOS 8.1)的方法:
步骤1-添加#import&lt;&#34; iAd / iAd.h&#34;&gt;在MyScene.h头文件中
步骤2-确保声明MyScene类以在MyScene.h头文件中实现协议。
步骤3-在MyScene.m文件中添加以下代码行 - (Void)didMoveToView:(SKView *)视图函数。
ADBannerView* banner=[[ADBannerView alloc]initWithFrame:CGRectZero];
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
banner.frame=bannerFrame;
[self.view addSubview:banner];
banner.delegate=self;
步骤4-实现iAd的两种方法
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
CGRect bannerFrame =CGRectMake(0, 667-50, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
CGRect bannerFrame =CGRectMake(0, 667, self.view.frame.size.width, 0);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
banner.frame=bannerFrame;
[UIView commitAnimations];
}
上述代码会在有广告时将广告框架移动到场景,如果没有广告,则会通过设置动画来移除广告框架。请注意,框架矩形中的最后一个数字是0.无论你放在那里都没关系,横幅高度是固定的,不会改变(50磅)。
步骤5-通过此代码回复广告操作:
-(void)bannerViewActionDidFinish:(ADBannerView *)banner
{
[self startTimer];
}
-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
[gameTimer invalidate];
return YES;
}
当用户点击横幅并在用户返回游戏后恢复游戏计时器时,此代码将停止游戏计时器。您可以在此处添加自己的代码以保存和检索游戏数据。
希望这有帮助。