Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SnapKit

import Then

class AdImageCollectionViewCell: UICollectionViewCell {
class AdImageCollectionViewCell: UICollectionViewCell, UIScrollViewDelegate {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delegate 관련 프로토콜은 가능하면 extension으로 빼서 채택시키기!
class 앞에 final 붙이기!


// MARK: - collectionview

Expand All @@ -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()
Expand All @@ -48,57 +52,62 @@ class AdImageCollectionViewCell: UICollectionViewCell {
// MARK: - Extensions

extension AdImageCollectionViewCell {

private func setDelegate() {
bannerCollectionView.delegate = self
bannerCollectionView.dataSource = self
bannerCollectionView.isPagingEnabled = true
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

func layout() {
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()
}
}

Expand All @@ -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
}
}

Expand Down