Swift:在Spritekit

时间:2015-06-18 23:50:40

标签: ios swift sprite-kit mopub

我正在使用MoPub横幅广告,我将以下代码添加到我的View Controller的viewDidLoad中:

     self.adView.delegate = self
    self.adView.frame = CGRectMake(0, self.view.bounds.size.height - MOPUB_BANNER_SIZE.height,
        MOPUB_BANNER_SIZE.width, MOPUB_BANNER_SIZE.height)
    self.view.addSubview(self.adView)
    self.adView.loadAd()

但是当我只希望它在主菜单场景中可见时,它会使广告在所有场景中都可见。

如何在我不想要的场景中删除广告?

1 个答案:

答案 0 :(得分:2)

这可能不是最好的方法,但它可能是最简单的方法。只要您希望展示或隐藏横幅,就可以使用NSNotificationViewController广播消息。

例如,如果你添加一个"观察者"在ViewController initviewDidLoad上的NSNotificationCenter.defaultCenter().addObserver( self, selector: "hideBannerAd", name: "hideAd", object: nil)

ViewController

"hideAd"侦听名为hideBannerAd的邮件,然后执行名为func hideBannerAd(){ self.adView.hidden = true } 的方法。

然后实现这个方法:

deinit

请务必删除ViewController上的观察者,SpriteKit deinit{ NSNotificationCenter.defaultCenter().removeObserver(self) } 的{​​{1}}的持续存在可能是个问题,但这不是问题。良好的做法。

hideBannerAd

然后,当您想要显示或隐藏视图时,例如在场景转换或游戏方法上,您可以通过使用以下方法触发观察者来实现此NSNotificationCenter.defaultCenter().postNotificationName("hideAd", object: nil) 方法:

showBannerAd

横幅应隐藏。然后,可以通过将hidden属性设置为false来为类似的adView.hidden = !adView.hidden方法重复此操作,或者您可以使用单个方法使用function setPlayingVideoToTrue(){playing_video = true;} function setPlayingVideoToFalse(){playing_video = false;} // check if a video iframe exists var iframe_videos = $('body').find('iframe'); if(iframe_videos.length > 0){ // create ready events for every iframe iframe_videos.each(function(index){ // add a temporary id for the iframe // append additional parameters to the end of the iframe's src var temporary_player_id = 'iframe_player_'+ index; var new_iframe_src = $(this).attr('src') +'?api=1&player_id='+ temporary_player_id; $(this).attr('id', temporary_player_id); $(this).attr('src', new_iframe_src); // add event listener for ready $f(this).addEvent('ready', iframe_ready); }); // when a player is ready, add event listeners for play, pause, finish, and playProgress function iframe_ready(player_id) { $f(player_id).addEvent('play', setPlayingVideoToTrue); $f(player_id).addEvent('playProgress', setPlayingVideoToTrue); $f(player_id).addEvent('pause', setPlayingVideoToFalse); $f(player_id).addEvent('finish', setPlayingVideoToFalse); } } function start_idle_timer(){ var timer = 0; function increment_duration() { if(isPaused === false) { timer++; } // increment timer if video is playing if(playing_video === true){ clearTimeout(idleTimer); isPaused = false; } if(playing_video === false && isPaused === false){ // stop timer if the user is idle for 3 minutes var idleTimer = setTimeout(function(){ // console.log('idle'); clearInterval(check_time); isPaused = true; // a modal will apper to inform that user is on idle state $('#linkage').trigger('click'); var modal_timer = 0; // timer for modal idle timer continue_modal_timer = setInterval(function(){ modal_timer++; inactivity_timer = modal_timer; if(modal_timer == 60) { clearInterval(continue_modal_timer); clearTimeout(idleTimer); } }, 1000) }, 10000); } // bind to all elements on DOM possible events indicating the user is active $('*').bind('mousemove click mouseup mousedown keydown keypress keyup submit change mouseenter scroll resize dblclick', function () { clearTimeout(idleTimer); isPaused = false; }); } // initialize the interval check_time = setInterval(increment_duration, 1000); } // check if start_timer is present from the loading page to record the time duration of the user if($('.start_timer').length > 0){ start_idle_timer(); } 简单地切换隐藏属性。

我希望这会有所帮助。