Skip to content

Commit

Permalink
#43 Fix: CollectionVIew , cell resize automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
doyeonk429 committed Aug 21, 2024
1 parent 598f63b commit 9143b63
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,109 @@ class ClassVideoViewController: UIViewController {

let youtubePlayer : YTPlayerView = YTPlayerView(frame: CGRect())

private let bottomView = UIView()
let xButton: UIButton = UIButton(type: .system)

private let videoTitleLabel = UILabel()
private let videoDespTitleLabel = UILabel()
private let videoDescriptionLabel = UILabel()

override func viewDidLoad() {
super.viewDidLoad()
self.tabBarController?.tabBar.isHidden = true

setupUI()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

// 레이아웃이 확정된 후 코너를 둥글게 설정
bottomView.layoutIfNeeded()
roundTopCorner(view: bottomView, cornerRadius: 30)
}

private func setupUI() {
youtubePlayer.load(withVideoId: ClassVideoViewController.videoData[0])
view.backgroundColor = .white

// youtubePlayer 설정
youtubePlayer.load(withVideoId: ClassVideoViewController.videoData[0], playerVars: ["playsinline": 0])
view.addSubview(youtubePlayer)
view.addSubview(bottomView)

youtubePlayer.snp.makeConstraints { make in
make.edges.equalTo(view.safeAreaLayoutGuide)
make.top.leading.trailing.equalTo(view.safeAreaLayoutGuide)
make.height.equalTo(view.snp.height).multipliedBy(0.8)
}

// bottomView 설정
bottomView.backgroundColor = UIColor.white
bottomView.snp.makeConstraints { v in
v.leading.trailing.bottom.equalTo(view.safeAreaLayoutGuide)
v.height.equalTo(view.snp.height).multipliedBy(0.2)
}

print("title : \(ClassVideoViewController.videoData[1]) \n 설명 : \(ClassVideoViewController.videoData[2])")
// xButton 설정
xButton.setImage(UIImage(systemName: "xmark"), for: .normal)
xButton.tintColor = .black
xButton.addTarget(self, action: #selector(dismissViewController), for: .touchUpInside)
bottomView.addSubview(xButton)
xButton.snp.makeConstraints { b in
b.top.equalTo(bottomView.snp.top).offset(36)
b.trailing.equalTo(bottomView).inset(16)
}

// videoTitleLabel 설정
videoTitleLabel.text = ClassVideoViewController.videoData[1]
videoTitleLabel.font = .boldSystemFont(ofSize: 22)
videoTitleLabel.textColor = .black
videoTitleLabel.numberOfLines = 0
bottomView.addSubview(videoTitleLabel)
videoTitleLabel.snp.makeConstraints { make in
make.top.equalTo(bottomView.snp.top).offset(36)
make.leading.equalTo(bottomView).inset(16)
make.trailing.equalTo(xButton.snp.leading)
}

videoDespTitleLabel.text = "영상 설명"
videoDespTitleLabel.font = .systemFont(ofSize: 16)
videoDespTitleLabel.textColor = .black
bottomView.addSubview(videoDespTitleLabel)
videoDespTitleLabel.snp.makeConstraints { make in
make.top.equalTo(videoTitleLabel.snp.bottom).offset(16)
make.leading.trailing.equalTo(bottomView).inset(16)
}

// videoDescriptionLabel 설정
videoDescriptionLabel.text = ClassVideoViewController.videoData[2]
videoDescriptionLabel.font = .systemFont(ofSize: 16)
videoDescriptionLabel.textColor = .darkGray
videoDescriptionLabel.numberOfLines = 0
bottomView.addSubview(videoDescriptionLabel)
videoDescriptionLabel.snp.makeConstraints { make in
make.top.equalTo(videoDespTitleLabel.snp.bottom).offset(4)
make.leading.trailing.equalTo(bottomView).inset(16)
}
}

@objc private func dismissViewController() {
self.navigationController?.popViewController(animated: true)
}

func roundTopCorner(view: UIView, cornerRadius: CGFloat) {
let path = UIBezierPath(
roundedRect: view.bounds,
byRoundingCorners: [.topRight, .topLeft], // 둥글게 만들 모서리 설정
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)
)
let mask = CAShapeLayer()
mask.path = path.cgPath
view.layer.mask = mask
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SnapKit
import Moya
import SDWebImage

class WineClassMainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate {
class WineClassMainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate, UICollectionViewDelegateFlowLayout {

let provider = MoyaProvider<WineClassAPI>(plugins: [CookiePlugin()])

Expand All @@ -26,7 +26,7 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,

var videoInfo : [WineClassResponse] = []

let ImageName: [String] = ["Castello Monaci", "Dos Copas", "Loxton", "Red Label", "Samos", "Vendredi"]
// let ImageName: [String] = ["Castello Monaci", "Dos Copas", "Loxton", "Red Label", "Samos", "Vendredi"]

lazy var searchBar: UISearchBar = {
let s = UISearchBar()
Expand Down Expand Up @@ -68,7 +68,6 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,
callGetAllClass { isSucess in
if isSucess {
self.setupAllUI()
print(self.videoInfo)
} else {
print("400 서버 에러")
}
Expand All @@ -94,14 +93,12 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,

private func callGetAllClass(completion: @escaping (Bool) -> Void) {
provider.request(.getAllWineClass) { result in
print(result)
switch result {
case .success(let response):
do {
let data = try response.map(APIResponseWineClassResponse.self)
print(data)
// print(data)
self.videoInfo = data.result
self.videoInfo += data.result
}
catch {
completion(false)
Expand Down Expand Up @@ -187,13 +184,12 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,
b.width.greaterThanOrEqualTo(30)
}


setupMainClassView()
containerView.addSubview(mainClassView)
mainClassView.snp.makeConstraints { make in
make.top.equalTo(button.snp.bottom).offset(10)
make.leading.trailing.equalTo(containerView).inset(10)
make.height.equalTo(UIConstants.classViewHeight) // 원하는 높이로 설정
make.height.equalTo(containerView.snp.height).multipliedBy(0.33)
}

bottomView.backgroundColor = .white // 원하는 배경색으로 변경
Expand All @@ -207,20 +203,19 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,
make.leading.trailing.bottom.equalToSuperview()
}


setupSubTitleClassLabel()
bottomView.addSubview(subtitleclassLabel)
subtitleclassLabel.snp.makeConstraints { make in
make.top.equalTo(bottomView.snp.top).offset(20)
make.leading.equalTo(bottomView.snp.leading).offset(20)
}

setupMainClassView()
bottomView.addSubview(mainClassView)
mainClassView.snp.makeConstraints { make in
setupMain2ClassView()
bottomView.addSubview(subClassView)
subClassView.snp.makeConstraints { make in
make.top.equalTo(subtitleclassLabel.snp.bottom).offset(7)
make.leading.trailing.equalTo(bottomView).inset(10)
make.height.equalTo(UIConstants.classViewHeight) // 원하는 높이로 설정
make.height.equalTo(mainClassView.snp.height)
}
}

Expand All @@ -234,7 +229,7 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,

func setupMainClassView() {
let layout = CarouselLayout()
layout.itemSize = UIConstants.mainClassViewItemSize
// layout.itemSize = UIConstants.mainClassViewItemSize
layout.spacing = UIConstants.mainClassViewSpacing

mainClassView = UICollectionView(frame: .zero, collectionViewLayout: layout)
Expand All @@ -246,6 +241,20 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,
mainClassView.register(WineClassCell.self, forCellWithReuseIdentifier: WineClassCell.reuseIdentifier)
}

func setupMain2ClassView() {
let layout = CarouselLayout()
// layout.itemSize = UIConstants.mainClassViewItemSize
layout.spacing = UIConstants.mainClassViewSpacing

subClassView = UICollectionView(frame: .zero, collectionViewLayout: layout)
subClassView.backgroundColor = .clear
subClassView.delegate = self
subClassView.dataSource = self
subClassView.showsHorizontalScrollIndicator = false
subClassView.showsVerticalScrollIndicator = false
subClassView.register(WineClassCell.self, forCellWithReuseIdentifier: WineClassCell.reuseIdentifier)
}

func setupSubTitleClassLabel() {
subtitleclassLabel.text = "이번 달 와인뉴스"
subtitleclassLabel.font = .boldSystemFont(ofSize: UIConstants.titleClassLabelFontSize)
Expand Down Expand Up @@ -305,5 +314,12 @@ class WineClassMainViewController: UIViewController, UICollectionViewDataSource,
ClassVideoViewController.videoData = [video.video, video.title, video.description]
navigationController?.pushViewController(controller, animated: true)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// 동적으로 계산된 크기를 반환합니다.
let height = collectionView.frame.height * 0.95

return CGSize(width: height*0.6, height: height)
}
}

0 comments on commit 9143b63

Please sign in to comment.