Dear members and Apple)
I'm try to make Carousel with PageControl by UIKit with One cell at a time with scrolling, Center it in the middle, at the same time Image must be smaller then CollectionView width, and I don't want to use any Pods (iCarousel also) or SwiftUI.
(PageControl I haven't connected yet in code)
I watched a tons of articles, video and questions (also on Stackoverflow).
I don't have much time(((
And I'm sure it helps a lot of people, because I've seen many questions like my, without correct answers(
Must be simple decision!
🙏🏼🙏🏼🙏🏼
What should I add to my code, give a hint, please?
Here is my result (.gif)
link on GIF
My CollectionView code:
class TopGalleryCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource {
var channels: [TopGalleryModel] = []
init() {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
super.init(frame: .zero, collectionViewLayout: layout)
backgroundColor = #colorLiteral(red: 0.113761507, green: 0.1048973277, blue: 0.150441885, alpha: 1)
delegate = self
dataSource = self
register(TopGalleryCollectionViewCell.self, forCellWithReuseIdentifier: TopGalleryCollectionViewCell.reuseId)
translatesAutoresizingMaskIntoConstraints = false
layout.minimumLineSpacing = Constants.galleryMinimumLineSpacing
contentInset = UIEdgeInsets(top: 0, left: Constants.leftDistanceToView, bottom: 0, right: Constants.rightDistanceToView)
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
isPagingEnabled = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Data Source
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return channels.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = dequeueReusableCell(withReuseIdentifier: TopGalleryCollectionViewCell.reuseId, for: indexPath) as! TopGalleryCollectionViewCell
cell.mainImageView.image = channels[indexPath.row].coverImage
cell.nameOfChannel.text = channels[indexPath.row].nameOfChannel
cell.numberOfSubscribers.text = channels[indexPath.row].numberOfSubscribers
return cell
}
// MARK: - Filling with content
func setContentFor(channels: [TopGalleryModel]) {
self.channels = channels
}
}
extension TopGalleryCollectionView: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: Constants.galleryItemWidth, height: frame.height)
}
}
Thanks in advance dear colleagues!)