将iAd添加到Sprite Kit Landscape

时间:2014-02-27 10:36:01

标签: ios objective-c sprite-kit

我在View Controller中有以下代码:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {
        skView.showsFPS = NO;
        skView.showsNodeCount = NO;
        skView.showsDrawCount = NO;

        // Create and configure the scene.
        SKScene * scene = [MenuScene sceneWithSize:skView.bounds.size];
       scene.scaleMode = SKSceneScaleModeAspectFill;

        self.canDisplayBannerAds = YES;

        // Present the scene.
        [skView presentScene:scene];
    }
}

当我运行应用程序时,它会立即崩溃。我收到以下错误消息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView scene]: unrecognized selector sent to instance

我无法使用SKView *skView = (SKView *)self.originalContentView;,因为该应用处于横向模式。有没有办法在Landscap Sprite Kit游戏中显示iAd?

编辑:

我刚刚将此代码添加到视图控制器中,但我得到了相同的结果..

#pragma mark iAd Delegate Methods

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [banner setAlpha:1];
    [UIView commitAnimations];
}

3 个答案:

答案 0 :(得分:3)

我遇到了这个问题并通过创建ABBannerView属性并将其添加为subView来解决它。

在我的ViewController类中:

adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.delegate = self;
[adView setFrame:CGRectMake(0, 0, 1024, 768)]; // set to your screen dimensions
[self.view addSubview:adView];

重要的是不要设置视图控制器的canDisplayBannerAds属性。

我相信发生的事情是,如果您将canDisplayBannerAds属性设置为true,则视图会被修改,并且不再与SKView兼容,并且不再具有场景属性。

我必须设置框架,以便尺寸正确,否则它是肖像。

答案 1 :(得分:1)

您无需手动配置广告横幅即可解决此问题。您仍然必须使用原始内容视图,但使其正常工作只是将所有内容放在视图控制器生命周期的正确阶段。您所要做的就是在awakeFromNib中启用canDisplayBannerAds :.然后根据原始内容视图的存在将您创建的SKView设置为视图控制器的视图或originalContentView。

- (void)awakeFromNib
{
    [super awakeFromNib];

    [self setCanDisplayBannerAds:YES];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

    SKView *skView = nil;

    if (self.originalContentView) {
        skView = (SKView *)self.originalContentView;
    }else{
        skView = (SKView *)self.view;
    }

    [skView setShowsDrawCount:YES];
    [skView setShowsFPS:YES];
    [skView setShowsNodeCount:YES];

    SKScene *scene = [MyScene sceneWithSize:skView.bounds.size];
    [scene setScaleMode:SKSceneScaleModeFill];

    [skView presentScene:scene];
}

答案 2 :(得分:1)

上述答案均不适合我。下面的代码在iOS 7和8中进行了测试,效果很好。

在标题文件

中添加以下行
#import <iAd/iAd.h>

@interface GameViewController : UIViewController<ADBannerViewDelegate>{

    //iAd
    ADBannerView *adView;

}

在实施文件.m中添加以下代码

#import "GameViewController.h"
#import "GameScene.h"

@implementation GameViewController

-(void)viewWillLayoutSubviews{

    [super viewWillLayoutSubviews];

    SKView *skView;

    if (self.originalContentView) {

        skView = (SKView *)self.originalContentView;

    }

    if (!skView.scene) {

        //[skView setShowsDrawCount:YES];
        //[skView setShowsFPS:YES];
        //[skView setShowsNodeCount:YES];

        //Improve Performance
        skView.ignoresSiblingOrder = YES;

        GameScene *scene = [GameScene sceneWithSize:skView.bounds.size];
        [scene setScaleMode:SKSceneScaleModeFill];

        [skView presentScene:scene];

    }

}

- (void)awakeFromNib{

    [super awakeFromNib];

    CGRect screenRect = [[UIScreen mainScreen] bounds];

    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectMake(0, 0, screenRect.size.width, adView.frame.size.height);
    adView.delegate=self;
    [self.view addSubview:adView];
}

//iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {

    [adView setAlpha:1.0];
    NSLog(@"Show Ad");

}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {

    [adView setAlpha:0];
    NSLog(@"Hide Ad");

}

//

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

-(BOOL)prefersStatusBarHidden{

    return YES;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

@end