我想将ADInterstitialAd视为常规UIView,以在使用时获得一定的灵活性。

例如,默认的插页式过渡是slide up from the bottom of the screen。我不喜欢是否可以先将alpha值修改为0,然后再将其更改为1?

另一个示例是在全屏屏幕前面添加不同的UIView一段短时间,即仅在ADInterstitialAd的过渡过程中? ADInterstitialAd往往是视图层次结构中最顶层的视图。我想如果可以将ADInterstitialAd视为常规UIView,就可以这样做。关于如何实现这一目标的任何建议?

最佳答案

您可以设置AdBannerView的alpha值的动画来实现所需的效果:

import iAd

private var bannerView: AdBannerView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Create your banner view however you want, and add it to your UI.
    bannerView = AdBannerView()
    bannerView.alpha = 0
    addSubview(bannerView)
}

// When you want to show the ad, animate the alpha.
private func animateInAd(appearing: Bool) {
    UIView.animateWithDuration(1.0) [weak self] {
        self?.bannerView.alpha = appearing ? 1 : 0
    }
}

这是呈现bannerView的另一种方法。完成向上滑动后,您可以删除广告顶部的视图:
import iAd

private var coverView: UIView!
private var bannerView: AdBannerView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Create your banner view however you want, and add it to your UI.
    bannerView = AdBannerView()
    addSubview(bannerView)

    // Create the cover view however you want, and add it above the banner view.
    coverView = UIView()
    addSubview(coverView)
}

private func animateInAd(appearing: Bool) {
    let hideAdPosition = view.frame.size.height
    let showAdPosition = hideAdPosition - bannerView.frame.size.height

    UIView.animateWithDuration(1.0, animations: [weak self] {
        self?.bannerView.frame.origin.y = appearing ? showAdPosition : hideAdPosition
    }) { [weak self] success in
        self?.animateCover(appearing: !appearing)
    }
}

private func animateCover(appearing: Bool) {
    UIView.animateWithDuration(1.0) [weak self] {
        self?.coverView.alpha = appearing ? 1 : 0
    }
}

编辑:

至于ADInterstitialAd,它们似乎是从NSObject继承的,并且具有显示它们所需的显式方法(presentInView:presentFromViewController:)。因此,没有公共API来控制这些广告的显示方式(这很可能是故意这样做的,以便Apple可以保证向用户展示广告)。

08-15 20:32