xcode 4.3 - storyboard - iAd一直在移动

时间:2012-03-22 10:55:14

标签: iphone objective-c xcode iad

我已将iAd添加到我的iphone应用程序中,位于我的应用程序的顶部。最初我将它放在x = 0和y = -50,以便它来自屏幕。我在.m中使用以下代码:

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

任何想法?

1 个答案:

答案 0 :(得分:2)

在这两种情况下,您都要将50.0px添加到banner.frame.origin.y

无论如何:即使你要在50.px中减去didFailToReceiveAdWithError: 可能会发生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;
    }
}