Skip to content

Commit

Permalink
[Feat] Runnect#134 - pagecontrol 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-yeonwoo2 committed May 12, 2023
1 parent cd1c350 commit da8536c
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class AdImageCollectionViewCell: UICollectionViewCell {
collectionView.showsVerticalScrollIndicator = false
return collectionView
}()

// MARK: - Constants

final let collectionViewInset = UIEdgeInsets(top: 28, left: 16, bottom: 28, right: 16)
Expand All @@ -33,7 +32,7 @@ class AdImageCollectionViewCell: UICollectionViewCell {
var imgBanners: [UIImage] = [ImageLiterals.imgBanner1, ImageLiterals.imgBanner2, ImageLiterals.imgBanner3]
var currentPage: Int = 0


private var pageControl = UIPageControl()
// MARK: - Life cycle
override init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -54,11 +53,7 @@ extension AdImageCollectionViewCell {
bannerCollectionView.isPagingEnabled = true
bannerCollectionView.showsHorizontalScrollIndicator = false
bannerCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "BannerCell")


}


func startBannerSlide() {

// 초기 페이지 설정
Expand All @@ -72,16 +67,21 @@ extension AdImageCollectionViewCell {
let swipeRightGesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGesture(_:)))
swipeRightGesture.direction = .right
bannerCollectionView.addGestureRecognizer(swipeRightGesture)

// 페이지 컨트롤 설정
pageControl.currentPage = 0
pageControl.numberOfPages = imgBanners.count
pageControl.pageIndicatorTintColor = .lightGray // 페이지를 암시하는 동그란 점의 색상
pageControl.currentPageIndicatorTintColor = .black
}

@objc func handleSwipeGesture(_ gesture: UISwipeGestureRecognizer) {
// 배너를 스와이프하여 수동으로 슬라이드
if gesture.direction == .left {
currentPage += 1
} else if gesture.direction == .right {
currentPage -= 1
}

// 첫 번째 배너에서 이전으로 스와이프하면 마지막 배너로 이동
if currentPage < 0 {
currentPage = imgBanners.count * 2 - 1
Expand All @@ -94,15 +94,29 @@ extension AdImageCollectionViewCell {

let indexPath = IndexPath(item: currentPage, section: 0)
bannerCollectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)

updatePageControl()
}

// 페이지 컨트롤 업데이트
func updatePageControl() {
let currentIndex = currentPage % imgBanners.count
pageControl.currentPage = currentIndex
}
// 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).offset(0)
}

}
}

Expand All @@ -122,7 +136,7 @@ extension AdImageCollectionViewCell: UICollectionViewDelegate, UICollectionViewD
imageView.image = imgBanners[imageIndex]
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
cell.contentView.addSubview(imageView)
cell.contentView.addSubviews(imageView)
return cell
}
}
Expand All @@ -137,5 +151,4 @@ extension AdImageCollectionViewCell: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

}

0 comments on commit da8536c

Please sign in to comment.