2012-03-22 62 views
2

我已將iAd添加到我的iPhone應用程序中,處於應用程序的頂部。最初我把它放在x = 0和y = -50的位置,以便它來自屏幕外。我用它下面的代碼在我的.m:xcode 4.3 - 故事板 - iAd不停地移動

- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error 
{ 
    if (self.bannerIsVisible) 
    { 
     [UIView beginAnimations:@"animateAdBannerOff" context:NULL]; 
     // Assumes the banner view is placed at the bottom of the screen. 
     banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height); 
     [UIView commitAnimations]; 
     self.bannerIsVisible = NO; 
    } 
} 

- (void)bannerViewDidLoadAd:(ADBannerView *)abanner 
{ 
    if (!self.bannerIsVisible) 
    { 
     [UIView beginAnimations:@"animateAdBannerOn" context:NULL]; 
     // Assumes the banner view is just off the bottom of the screen. 
     banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height); 
     [UIView commitAnimations]; 
     self.bannerIsVisible = YES; 
    } 
} 

當我的應用程序推出的iAd獲得顯示在頂部沒有任何問題。但是當我打開另一個應用程序並回到它(沒有殺死它,所以我的應用程序在後臺運行)橫幅移動另一個50像素向下enter image description here

任何想法?

回答

2

在這兩種情況下,您都將50.0px添加到banner.frame.origin.y

總之:即使你在didFailToReceiveAdWithError:被從其減去50.px它 可能發生didFailToReceiveAdWithError:將在 行被調用多次,你的代碼可以移動的旗幟越來越高了(-50.0,-100.0, - 150.0 ...)。

因此,最好將隱藏的&可見位置硬編碼,而不是計算它。

試試這個:

- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error 
{ 
    if (self.bannerIsVisible) 
    { 
     [UIView beginAnimations:@"animateAdBannerOff" context:NULL]; 
     banner.frame = CGRectMake(0.0,-50.0,banner.frame.size.width,banner.frame.size.height); 
     [UIView commitAnimations]; 
     self.bannerIsVisible = NO; 
    } 
} 

- (void)bannerViewDidLoadAd:(ADBannerView *)abanner 
{ 
    if (!self.bannerIsVisible) 
    { 
     [UIView beginAnimations:@"animateAdBannerOn" context:NULL]; 
     banner.frame = CGRectMake(0.0,0.0,banner.frame.size.width,banner.frame.size.height); 
     [UIView commitAnimations]; 
     self.bannerIsVisible = YES; 
    } 
} 
+0

硬編碼它留下了白色的空白欄時,沒有廣告。這就是爲什麼我最初將它從屏幕上移開,然後在有廣告顯示時將其移開。 – Adeeb 2012-03-22 13:12:05

+0

我的意思是hardcode它已關閉,它的位置 - 不要把它放在固定的地方。你嘗試了建議的代碼嗎? – 2012-03-22 13:15:25

+0

優秀!!謝謝 – Adeeb 2012-03-22 14:06:55