【问题标题】:Hide iAd in Sprite Kit Scene在 Sprite Kit 场景中隐藏 iAd
【发布时间】:2014-02-27 19:21:08
【问题描述】:

我已使用以下代码将 iAd 添加到我的 Sprite Kit 游戏中:

在 viewController.h 文件中

@property (strong, nonatomic) IBOutlet ADBannerView * adBannerView;

在 viewController.m 文件中

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];

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

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

        _adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
        _adBannerView.delegate = self;
        [_adBannerView setFrame:CGRectMake(0, 0, 460, 320)];
        [self.view addSubview:_adBannerView];

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

这会在每个场景中显示 iAd。有没有办法在某些场景中隐藏 iAd?

Apple 的 iAd 编程指南说:

仅当您打算向用户显示横幅视图时才创建它。否则,它可能会循环播放广告并耗尽您的应用程序的可用广告列表。

在场景中这完全可能吗?

【问题讨论】:

    标签: ios objective-c sprite-kit


    【解决方案1】:

    是的,有一种方法可以在某些场景中隐藏 iAd。

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

    【讨论】:

      【解决方案2】:

      好吧,在您的特定场景中,请按照以下链接中有关此问题的 Apple 指南(与您的问题相同的位置),查看“横幅视图最佳实践”部分: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html#//apple_ref/doc/uid/TP40009881-CH4-SW3

      摘要中他们说:“从视图层次结构中删除横幅视图,将其委托设置为nil

      【讨论】:

        【解决方案3】:

        最干净的解决方案是声明并实现一个协议,让UIViewController 从场景中知道它应该隐藏广告。

        @protocol MySceneDelegate <NSObject>
        - (void)hideAd;
        @end
        
        @interface MyScene : SKScene
        @property (weak) id <MySceneDelegate> delegate;
        @end
        

        显示场景的视图控制器应该实现hideAd 方法并将自己设置为场景的委托。示例:

        - (void)viewDidLoad
        {
            [super viewDidLoad];
        
            // Configure the view.
            SKView * skView = (SKView *)self.view;
            skView.showsFPS = YES;
            skView.showsNodeCount = YES;
        
            // Create and configure the scene.
            MyScene * scene = [MyScene sceneWithSize:skView.bounds.size];
            scene.scaleMode = SKSceneScaleModeAspectFill;
        
            // Set the delegate
            [scene setDelegate:self];
        
            // Present the scene.
            [skView presentScene:scene];
        }
        

        然后在场景中调用被设置为委托的视图控制器的hideAd方法:

        if ([_delegate respondsToSelector:@selector(closeScene)])
        {
            [_delegate performSelector:@selector(hideAd)];
        }
        

        并删除hideAd 方法中的横幅。

        要隐藏横幅视图,您应该:

        将横幅视图的框架调整为屏幕外调整内容的大小 视图的框架以覆盖最初承载横幅的空间

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-27
          • 1970-01-01
          相关资源
          最近更新 更多