在 Spritekit 中隐藏/显示 iAd [英] Hide/Show iAds in Spritekit

查看:27
本文介绍了在 Spritekit 中隐藏/显示 iAd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been trying to figure out how to hide and show iAds in my Spritekit Scenes. Currently I have it setup like this:

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

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

然后在我的场景中,我用指针抓住我的视图控制器:

Then in my scene I grab my viewcontroller with a pointer:

ViewController *controller;


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

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

My nslog runs in the console so I know it's going through. But the banner won't hide. Any thoughts? I'm pretty new with objective c so I have a feeling I'm just doing something dumb.

推荐答案

就像 Huygamer 说的,你正在创建一个视图控制器的新实例,所以当你调用你的方法时 [controller hidesBanner];你指的是另一个对象.

Like Huygamer said, you're creating a new instance of a view controller so when you call your method [controller hidesBanner]; you're referring to another object.

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

The best approach here is to use NSNotificationCenter: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

并在您想要隐藏或显示广告时向您的视图控制器发送消息:

And send a message to your viewcontroller whenever you want to hide or show your ad:

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.

这篇关于在 Spritekit 中隐藏/显示 iAd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆