我有一段时间想知道同样的事情。我相信我想出了一个技巧,因为我这样做时没有收到任何警告。
这是我的视图层次结构:
UIView (same size as your iAd)
|_ iAd (make sure to pin the height and width if using iOS 6 auto layout)
|_ UIView (I create this dynamically and use it's presence to determine whether I should show or hide the iAd from the delegate)
下面的代码操作我在 Interface Builder 中设置的自动布局约束。如果您不使用自动布局,则必须更改触发动画的内容。
- (void)hideAdBanner {
if (!__adBannerReverseSideView) {
__adBannerReverseSideView = [[UIView alloc] initWithFrame:__adBannerView.frame];
__adBannerReverseSideView.backgroundColor = [UIColor blackColor];
__adBannerReverseSideView.opaque = YES;
[UIView transitionFromView:__adBannerView toView:__adBannerReverseSideView duration:0.3
options:UIViewAnimationOptionTransitionFlipFromBottom | UIViewAnimationOptionCurveEaseInOut
completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
__adBannerBottomConstraint.constant += __adBannerContainerView.frame.size.height;
[self.view layoutIfNeeded];
}];
}];
}
}
- (void)showAdBanner {
if (__adBannerReverseSideView) {
[UIView animateWithDuration:0.3
animations:^{
__adBannerBottomConstraint.constant -= __adBannerContainerView.frame.size.height;
[self.view layoutIfNeeded];
}
completion:^(BOOL finished) {
[__adBannerView setNeedsLayout];
[UIView transitionFromView:__adBannerReverseSideView toView:__adBannerView duration:0.3
options:UIViewAnimationOptionTransitionFlipFromTop | UIViewAnimationOptionCurveEaseInOut
completion:^(BOOL finished) {
[__adBannerReverseSideView removeFromSuperview];
__adBannerReverseSideView = nil;
}];
}];
}
}
隐藏代码将广告横幅转换为“反向”视图。您可以使用 options 参数更改动画类型。
显示代码以另一种方式转换(从“反向”视图到广告横幅)。所有的动画都发生在与广告横幅大小相同的超级视图上。这样你的整个视图就不会动画了。
将 iAd 留在 superview 中,不要删除它。这可能是警告的根本原因,但我不确定。
然后这是我的 AD 委托方法:
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[self hideAdBanner];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
[self showAdBanner];
}
不要因为我没有检查错误变量而杀了我。我还没来得及写那段代码。
关于在 iOS 6 自动布局中固定广告横幅视图的高度和宽度,如果您不这样做,当 iAd 动画回到原位时,左上角会向下和向右移动一半每次返回视图时,其父视图的高度和宽度。 :) 有趣的东西。