From 5037d2b97f629337aaee08710c532bb4bfce0375 Mon Sep 17 00:00:00 2001 From: yeonwoo Date: Sun, 28 May 2023 23:33:50 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[Fix]=20#153=20-=20=EB=B0=B0=EB=84=88=20?= =?UTF-8?q?=EC=88=98=EB=8F=99=20=EC=8A=A4=ED=81=AC=EB=A1=A4=20=EC=8B=9C=20?= =?UTF-8?q?=ED=8E=98=EC=9D=B4=EC=A7=80=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Views/AdImageCollectionViewCell.swift | 57 ++++++++++--------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift index 14e594a6..77ffa661 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 @@ -55,36 +55,24 @@ 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() { + pageControl.currentPage = 0 + pageControl.numberOfPages = imgBanners.count + pageControl.pageIndicatorTintColor = .lightGray // 페이지를 암시하는 동그란 점의 색상 + pageControl.currentPageIndicatorTintColor = .white + } + private func updatePageControl() { let currentIndex = currentPage % imgBanners.count pageControl.currentPage = currentIndex - } + } + internal func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { + let currentPage = Int(scrollView.contentOffset.x / scrollView.frame.width) + pageControl.currentPage = currentPage % imgBanners.count + } // MARK: - Layout Helpers @@ -96,12 +84,27 @@ extension AdImageCollectionViewCell { 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 + } + let indexPath = IndexPath(item: currentPage, section: 0) + bannerCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false) + updatePageControl() + } + +} + // MARK: - UICollectionViewDelegate, UICollectionViewDataSource extension AdImageCollectionViewCell: UICollectionViewDelegate, UICollectionViewDataSource { From 82597b6dda3a7f22cf636875b13f18b6adecf5e8 Mon Sep 17 00:00:00 2001 From: yeonwoo Date: Sun, 28 May 2023 23:39:28 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[Fix]=20#153=20-=20=EA=B3=B5=EB=B0=B1=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Views/AdImageCollectionViewCell.swift | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift index 77ffa661..074ed26a 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/AdImageCollectionViewCell.swift @@ -24,17 +24,21 @@ class AdImageCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate { 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, UIScrollViewDelegate { // MARK: - Extensions extension AdImageCollectionViewCell { + private func setDelegate() { bannerCollectionView.delegate = self bannerCollectionView.dataSource = self @@ -55,20 +60,23 @@ extension AdImageCollectionViewCell { bannerCollectionView.showsHorizontalScrollIndicator = false bannerCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "BannerCell") } + 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.pageIndicatorTintColor = .lightGray pageControl.currentPageIndicatorTintColor = .white } + private func updatePageControl() { - let currentIndex = currentPage % imgBanners.count - pageControl.currentPage = currentIndex + 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 @@ -80,6 +88,7 @@ extension AdImageCollectionViewCell { contentView.backgroundColor = .clear contentView.addSubview(bannerCollectionView) contentView.addSubview(pageControl) + bannerCollectionView.snp.makeConstraints { make in make.edges.equalToSuperview() } @@ -94,15 +103,12 @@ extension AdImageCollectionViewCell { extension AdImageCollectionViewCell { @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: false) + currentPage += 1 + if currentPage >= imgBanners.count { + currentPage = 0 + } updatePageControl() } - } // MARK: - UICollectionViewDelegate, UICollectionViewDataSource @@ -114,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 } }