diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift index 14e594a6..074ed26a 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift @@ -10,7 +10,7 @@ import SnapKit import Then -class AdImageCollectionViewCell: UICollectionViewCell { +class AdImageCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate { // MARK: - collectionview @@ -24,17 +24,21 @@ class AdImageCollectionViewCell: UICollectionViewCell { collectionView.showsVerticalScrollIndicator = false return collectionView }() + // MARK: - Constants final let collectionViewInset = UIEdgeInsets(top: 28, left: 16, bottom: 28, right: 16) // MARK: - UI Components + var imgBanners: [UIImage] = [ImageLiterals.imgBanner1, ImageLiterals.imgBanner2, ImageLiterals.imgBanner3] var currentPage: Int = 0 private var timer: Timer? private var pageControl = UIPageControl() + // MARK: - Life cycle + override init(frame: CGRect) { super.init(frame: frame) layout() @@ -48,6 +52,7 @@ class AdImageCollectionViewCell: UICollectionViewCell { // MARK: - Extensions extension AdImageCollectionViewCell { + private func setDelegate() { bannerCollectionView.delegate = self bannerCollectionView.dataSource = self @@ -55,36 +60,27 @@ extension AdImageCollectionViewCell { bannerCollectionView.showsHorizontalScrollIndicator = false bannerCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "BannerCell") } - func startBannerSlide() { - - // 초기 페이지 설정 + + private func startBannerSlide() { currentPage = imgBanners.count timer = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(animateBannerSlide), userInfo: nil, repeats: true) - - // 페이지 컨트롤 설정 - pageControl.currentPage = 0 - pageControl.numberOfPages = imgBanners.count - pageControl.pageIndicatorTintColor = .lightGray // 페이지를 암시하는 동그란 점의 색상 - pageControl.currentPageIndicatorTintColor = .white - } - @objc func animateBannerSlide() { - currentPage += 1 - - if currentPage >= imgBanners.count { - currentPage = 0 - } - - let indexPath = IndexPath(item: currentPage, section: 0) - bannerCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) - - pageControl.currentPage = currentPage - } - - // 페이지 컨트롤 업데이트 - func updatePageControl() { - let currentIndex = currentPage % imgBanners.count - pageControl.currentPage = currentIndex - } + pageControl.currentPage = 0 + pageControl.numberOfPages = imgBanners.count + pageControl.pageIndicatorTintColor = .lightGray + pageControl.currentPageIndicatorTintColor = .white + } + + private func updatePageControl() { + let currentIndex = currentPage % imgBanners.count + pageControl.currentPage = currentIndex + let indexPath = IndexPath(item: currentPage, section: 0) + bannerCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) + } + + internal func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { + let currentPage = Int(scrollView.contentOffset.x / scrollView.frame.width) + pageControl.currentPage = currentPage % imgBanners.count + } // MARK: - Layout Helpers @@ -92,13 +88,26 @@ extension AdImageCollectionViewCell { contentView.backgroundColor = .clear contentView.addSubview(bannerCollectionView) contentView.addSubview(pageControl) + bannerCollectionView.snp.makeConstraints { make in make.edges.equalToSuperview() } pageControl.snp.makeConstraints { make in - make.centerX.equalTo(self) - make.bottom.equalTo(bannerCollectionView.snp.bottom) + make.centerX.equalTo(self) + make.bottom.equalTo(bannerCollectionView.snp.bottom) + } + } +} + +// MARK: - @objc Function + +extension AdImageCollectionViewCell { + @objc func animateBannerSlide() { + currentPage += 1 + if currentPage >= imgBanners.count { + currentPage = 0 } + updatePageControl() } } @@ -111,15 +120,13 @@ extension AdImageCollectionViewCell: UICollectionViewDelegate, UICollectionViewD func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = bannerCollectionView.dequeueReusableCell(withReuseIdentifier: "BannerCell", for: indexPath) - - // 배너 이미지 설정 - let imageIndex = indexPath.item % imgBanners.count - let imageView = UIImageView(frame: cell.contentView.bounds) - imageView.image = imgBanners[imageIndex] - imageView.contentMode = .scaleAspectFill - imageView.clipsToBounds = true - cell.contentView.addSubviews(imageView) - return cell + let imageIndex = indexPath.item % imgBanners.count + let imageView = UIImageView(frame: cell.contentView.bounds) + imageView.image = imgBanners[imageIndex] + imageView.contentMode = .scaleAspectFill + imageView.clipsToBounds = true + cell.contentView.addSubviews(imageView) + return cell } }