Skip to content

Commit

Permalink
feat/#64 Banner Img 네트워크 연결 - kf 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
HEHEEUN committed Nov 28, 2024
1 parent 633da6e commit 17fb61a
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 2 deletions.
4 changes: 2 additions & 2 deletions saftyReport/saftyReport/Source/Main/Cell/BannerCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class BannerCell: UICollectionViewCell {let bannerImageView = UIImageView().then
}

func configure(image: String) {
if let image = UIImage(named: image) {
bannerImageView.image = image
if let url = URL(string: image) {
bannerImageView.kf.setImage(with: url)
}
}
}
160 changes: 160 additions & 0 deletions saftyReport/saftyReport/Source/Main/Cell/MyReportBannerCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//
// MyReportBannerCell.swift
// saftyReport
//
// Created by 김희은 on 11/25/24.
//

import UIKit

class MyReportBannerCell: UICollectionViewCell {
var bannerImgList = ["img_promotion_1", "img_promotion_2", "img_promotion_3"]

lazy var leftButton = UIButton().then {
$0.setBackgroundImage(UIImage.icnArrowLeftRoundWhite24Px, for: .normal)
$0.addTarget(self, action: #selector(leftButtonTapped), for: .touchUpInside)
}

lazy var rightButton = UIButton().then {
$0.setBackgroundImage(UIImage.icnArrowRightRoundWhite24Px, for: .normal)
$0.addTarget(self, action: #selector(rightButtonTapped), for: .touchUpInside)
}

let layout = UICollectionViewFlowLayout().then {
$0.scrollDirection = .horizontal
$0.minimumLineSpacing = 0
$0.minimumInteritemSpacing = 0
$0.footerReferenceSize = .zero
$0.headerReferenceSize = .zero
}

lazy var collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout).then {
$0.layer.cornerRadius = 15
$0.clipsToBounds = true

$0.isScrollEnabled = true
$0.isPagingEnabled = true
$0.showsHorizontalScrollIndicator = false
$0.backgroundColor = .clear
}

lazy var pageControl = UIPageControl().then {
$0.numberOfPages = bannerImgList.count
$0.currentPage = 0
$0.currentPageIndicatorTintColor = .gray1
$0.pageIndicatorTintColor = .gray1Opacity30
$0.backgroundColor = .gray13Opacity40
$0.layer.cornerRadius = 5

let dotImage = UIImage.ellipse5X5.withConfiguration(
UIImage.SymbolConfiguration(pointSize: 5)
)
$0.preferredIndicatorImage = dotImage

$0.addTarget(self, action: #selector(pageControlTapped(_:)), for: .valueChanged)
}

override init(frame: CGRect) {
super.init(frame: frame)

setUI()
setLayout()
setCollectionView()
}


required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

private func setUI() {
self.addSubview(collectionView)
self.addSubview(pageControl)
self.addSubviews(leftButton, rightButton)
}

private func setLayout() {
collectionView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
pageControl.snp.makeConstraints {
$0.bottom.equalToSuperview().inset(10)
$0.centerX.equalToSuperview()
$0.width.equalTo(59)
$0.height.equalTo(15)
}
leftButton.snp.makeConstraints {
$0.leading.equalToSuperview().inset(4)
$0.centerY.equalToSuperview()
}
rightButton.snp.makeConstraints {
$0.trailing.equalToSuperview().inset(4)
$0.centerY.equalToSuperview()
}
}

private func setCollectionView(){
collectionView.dataSource = self
collectionView.delegate = self

collectionView.register(BannerCell.self, forCellWithReuseIdentifier: BannerCell.cellIdentifier)
}

private func scrollToCurrentPage() {
let indexPath = IndexPath(item: pageControl.currentPage, section: 0)
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}

@objc private func leftButtonTapped(_ sender: UIButton) {
pageControl.currentPage -= 1
scrollToCurrentPage()
}

@objc private func rightButtonTapped(_ sender: UIButton) {
pageControl.currentPage += 1
scrollToCurrentPage()
}

@objc private func pageControlTapped(_ sender: UIPageControl) {
let currentPage = sender.currentPage
let indexPath = IndexPath(item: currentPage, section: 0)
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}

func configure(bannerListImgUrl: [String]) {
bannerImgList = bannerListImgUrl

collectionView.reloadData()
}
}

extension MyReportBannerCell: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = Int(scrollView.contentOffset.x / collectionView.frame.width)
pageControl.currentPage = page
}
}

extension MyReportBannerCell: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
pageControl.numberOfPages = bannerImgList.count
return self.pageControl.numberOfPages
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BannerCell.cellIdentifier, for: indexPath) as? BannerCell else {
return UICollectionViewCell(frame: .zero)
}
cell.configure(image: bannerImgList[indexPath.row])
return cell
}
}

extension MyReportBannerCell: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
}
5 changes: 5 additions & 0 deletions saftyReport/saftyReport/Source/Main/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MainViewController: UIViewController {
var monthReportCount: Int = 0
var milieage: Int = 0
var bannerList: [BannerList] = []
var bannerListImgUrl: [String] = []

let customNavigationItem = CustomNavigationItem(title: "") // 반드시 타이틀 설정

Expand Down Expand Up @@ -219,6 +220,9 @@ class MainViewController: UIViewController {
monthReportCount = response.monthReportCount ?? 0
milieage = response.mileage ?? 0
bannerList = response.bannerList
for banner in bannerList {
bannerListImgUrl.append(banner.bannerUrl ?? "")
}
case let .failure(error):
print(error.localizedDescription)

Expand Down Expand Up @@ -302,6 +306,7 @@ extension MainViewController: UICollectionViewDataSource {
) as? MyReportBannerCell else {
return UICollectionViewCell(frame: .zero)
}
cell.configure(bannerListImgUrl: bannerListImgUrl)
return cell
case 2:
guard let cell = collectionView.dequeueReusableCell(
Expand Down

0 comments on commit 17fb61a

Please sign in to comment.