我正在使用 UICollectionView,它应该像轮播一样工作。我想显示上一个单元格的某些部分 - 当前单元格 - 下一个单元格的某些部分。我已经尝试过项目大小和sectionInsets-Left和Right,但没有任何效果。我恢复了所有的尝试,从头开始。您能否建议我应该更改哪些内容才能使其正常工作。
let mainCarouselCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 300, height: 150), collectionViewLayout: UICollectionViewFlowLayout())
override func viewDidLoad() {
super.viewDidLoad()
mainCarouselCollectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: CustomCollectionViewCell.reuseIdentifier)
mainCarouselCollectionView.dataSource = self
mainCarouselCollectionView.tag = 0
view.addSubview(mainCarouselCollectionView)
startTimerForMainCarousel(scrollTime: 2)
}
override func viewDidLayoutSubviews() {
let width = UIScreen.main.bounds.width
let mainCollectionViewFlowControl = UICollectionViewFlowLayout()
mainCollectionViewFlowControl.itemSize = CGSize(width: width - 10, height: 218)
mainCollectionViewFlowControl.scrollDirection = UICollectionView.ScrollDirection.horizontal
mainCollectionViewFlowControl.minimumLineSpacing = 10
//mainCollectionViewFlowControl.minimumInteritemSpacing = 0
mainCollectionViewFlowControl.sectionInset = UIEdgeInsets(top: 0, left:5, bottom: 0, right: 5)
self.mainCarouselCollectionView.collectionViewLayout = mainCollectionViewFlowControl
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let carouselCell = collectionView.dequeueReusableCell(
withReuseIdentifier: CustomCollectionViewCell.reuseIdentifier,
for: indexPath) as? CustomCollectionViewCell
else { fatalError("Cannot create new cell") }
let subViews = carouselCell.parentView.subviews
for subview in subViews{
subview.removeFromSuperview()
}
let carouselView : UIView = UIView()
carouselView.frame.origin.x = 4
carouselView.frame.origin.y = 4
carouselView.frame.size.height = carouselCell.parentView.frame.size.height - 8
carouselView.frame.size.width = carouselCell.parentView.frame.size.width - 8
carouselView.backgroundColor = .blue
carouselCell.parentView.addSubview(carouselView)
return carouselCell
}
func startTimerForMainCarousel(scrollTime: Int ) {
DispatchQueue.main.async { [self] in
self.mainCarouselTimer?.invalidate()
self.mainCarouselTimer = nil
self.mainCarouselTimer = Timer.scheduledTimer(timeInterval: TimeInterval(scrollTime), target: self, selector: #selector(automaticScrollMainCarousel), userInfo: nil, repeats: true)
}
}
@objc func automaticScrollMainCarousel() {
if mainCarouselCounter < mainCarouselArray.count - 1 {
mainCarouselCounter += 1
mainCarouselCollectionView.scrollToItem(at: IndexPath(item: mainCarouselCounter, section: 0), at: .centeredHorizontally, animated: true)
} else {
mainCarouselCounter = 0
mainCarouselCollectionView.scrollToItem(at: IndexPath(item: mainCarouselCounter, section: 0), at: .centeredHorizontally, animated: false)
}
}
class CustomCollectionViewCell: UICollectionViewCell {
let parentView = UIView()
static let reuseIdentifier = "cell-reuse-identifier"
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder: NSCoder) {
fatalError("not implemented")
}
func configure() {
parentView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(parentView)
NSLayoutConstraint.activate([
parentView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
parentView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
parentView.topAnchor.constraint(equalTo: contentView.topAnchor),
parentView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
}