Copy class PremiumBannerFlowLayoutViewController: UIViewController {
private var collectionView: UICollectionView!
private var premiumBannerAdView: NestAdsPremiumBannerAdView?
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
loadAd()
}
private func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = 16
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(PremiumBannerCell.self, forCellWithReuseIdentifier: "AdCell")
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
private func loadAd() {
let adView = NestAdsPremiumBannerAdView(
defaultBannerAdSize: NestAdsAdSizeFluid,
expandedBannerAdSize: NestAdsAdSizeFluid
)
adView.placementCode = "YOUR_PLACEMENT_CODE"
adView.delegate = self
premiumBannerAdView = adView
adView.load(NestAdsAdRequest())
}
}
// MARK: - UICollectionViewDataSource
extension PremiumBannerFlowLayoutViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdCell", for: indexPath) as! PremiumBannerCell
if let adView = premiumBannerAdView {
cell.setAdView(adView)
}
return cell
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension PremiumBannerFlowLayoutViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width - 32 // 좌우 여백
if let adView = premiumBannerAdView {
// SDK의 intrinsicContentSize를 사용하여 높이 계산
let height = adView.intrinsicContentSize.height > 0 ? adView.intrinsicContentSize.height : 100
return CGSize(width: width, height: height)
}
return CGSize(width: width, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
}
}
// MARK: - NestAdsPremiumBannerAdViewDelegate
extension PremiumBannerFlowLayoutViewController: NestAdsPremiumBannerAdViewDelegate {
func premiumAdViewDidReceiveAd(_ adView: NestAdsPremiumBannerAdView) {
collectionView.collectionViewLayout.invalidateLayout()
}
func premiumAdViewDidExpand(_ adView: NestAdsPremiumBannerAdView) {
collectionView.collectionViewLayout.invalidateLayout()
}
func premiumAdViewExpandedRectInfoOnScrollView(_ adView: NestAdsPremiumBannerAdView, rect: CGRect) {
var adjustedRect = rect
adjustedRect.size.height += 20
collectionView.scrollRectToVisible(adjustedRect, animated: true)
}
func premiumAdViewDidShrink(_ adView: NestAdsPremiumBannerAdView) {
collectionView.collectionViewLayout.invalidateLayout()
}
// 나머지 필수 delegate 메서드 구현...
func premiumAdView(_ adView: NestAdsPremiumBannerAdView, didFailToReceiveAdWithError error: NestAdsAdError) {}
func premiumAdViewDidRecordImpressionOnDefaultView(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewDidRecordImpressionOnExpandedView(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewDidPerformClickOnDefaultView(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewDidPerformClickOnExpandedView(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewDidPerformClickOnCtaButton(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewDidPerformClickOnHintAsset(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewWillPresentScreen(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewWillDismissScreen(_ adView: NestAdsPremiumBannerAdView) {}
func premiumAdViewDidDismissScreen(_ adView: NestAdsPremiumBannerAdView) {}
func didTapCtaButton(_ adView: NestAdsPremiumBannerAdView) {}
func didTapAdvertiser(_ adView: NestAdsPremiumBannerAdView) {}
func didTapAdProfile(_ adView: NestAdsPremiumBannerAdView) {}
func expandedVideoDidCompletePreparation() {}
func expandedVideoDidFailToLoadVideo(errorDescription: String) {}
func expandedVideoDidPlayVideo() {}
func expandedVideoDidPauseVideo() {}
func expandedVideoDidEndVideoPlayback() {}
func expandedVideoDidMuteVideo() {}
func expandedVideoDidUnmuteVideo() {}
func expandedVideoDidUpdateVideoProgress(current: TimeInterval) {}
}
// MARK: - Cell
class PremiumBannerCell: UICollectionViewCell {
private weak var adView: UIView?
func setAdView(_ view: UIView) {
// 같은 뷰면 스킵
if adView === view { return }
adView?.removeFromSuperview()
adView = view
view.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(view)
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: contentView.topAnchor),
view.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
view.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
}