UIPageControl的无限UICollectionview
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIPageControl的无限UICollectionview相关的知识,希望对你有一定的参考价值。
在下面给出的代码中,我可以简单地无限滚动单元格。页面控件似乎至少在向前滚动(屏幕右侧)时起作用。但坦率地说,当我滚动到左侧时,我看到3个项目的奇怪的索引路径跳转(顺便说一下,我的数据源有3个项目用于测试目的)。在屏幕上我发现没有错。但页面控制似乎暂时冻结,并开始再次工作,但有一个班次,并显示单元格的错误点。有任何想法吗?谢谢你的帮助...
import UIKit
class HomeHeaderCell: CategoryCell {
private let cellId = "cellId"
private var timer: Timer?
private let infiniteSize = 1000
private var onlyOnce = true
// ...
let pageControl: UIPageControl = {
let rect = CGRect(origin: .zero, size: CGSize(width: 200, height: 50))
let pc = UIPageControl(frame: rect)
pc.currentPage = 0
pc.numberOfPages = 3
pc.pageIndicatorTintColor = .gray
pc.currentPageIndicatorTintColor = .red
pc.isUserInteractionEnabled = false
return pc
}()
override func setupViews() {
addSubview(baseCollectionView)
addSubview(pageControl)
baseCollectionView.delegate = self
baseCollectionView.dataSource = self
baseCollectionView.register(HeaderCell.self, forCellWithReuseIdentifier: cellId)
baseCollectionView.backgroundColor = .white
baseCollectionView.isPagingEnabled = true
baseCollectionView.isScrollEnabled = true
baseCollectionView.showsHorizontalScrollIndicator = false
baseCollectionView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor)
pageControl.anchor(top: nil, leading: leadingAnchor, bottom: bottomAnchor, trailing: nil, padding: UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 0))
}
@objc func autoScroll() {
guard let currentItemNumber = baseCollectionView.indexPathsForVisibleItems.first?.item else { return }
let nextItemNumber = currentItemNumber + 1
let nextIndexPath = IndexPath(item: nextItemNumber, section: 0)
baseCollectionView.scrollToItem(at: nextIndexPath, at: .left, animated: true)
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
stopTimer()
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
startTimer()
}
func startTimer() {
timer = Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: #selector(autoScroll), userInfo: nil, repeats: true)
}
func stopTimer() {
timer?.invalidate()
timer = nil
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return infiniteSize
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! HeaderCell
// Maybe can be done more elegantly...
if let foods = foodCategory?.foods {
let numberOfFood = indexPath.item % foods.count
cell.food = foods[numberOfFood]
pageControl.currentPage = numberOfFood
print(numberOfFood)
}
return cell
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if onlyOnce {
let middleIndex = IndexPath(item: Int (infiniteSize / 2), section: 0)
baseCollectionView.scrollToItem(at: middleIndex, at: .centeredHorizontally, animated: false)
startTimer()
onlyOnce = false
}
}
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: frame.width, height: frame.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
private class HeaderCell: ItemCell {
override func setupViews() {
addSubview(imageView)
imageView.layer.cornerRadius = 0
imageView.layer.borderColor = UIColor(white: 0.5, alpha: 0.5).cgColor
imageView.layer.borderWidth = 0.5
imageView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor)
}
}
}
答案
我解决了自己的问题。这是您应该应用的代码。前两个方法是UIScrollView方法,它们随UICollectionView一起提供,它通常是UIScrollView的子类。 updatePageControl方法是我为了保持代码干净简单而编写的方法。希望这可以帮助...
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
updatePageControl(scrollView: scrollView)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
updatePageControl(scrollView: scrollView)
}
func updatePageControl(scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.bounds.size.width)
guard let count = foodCategory?.foods?.count else {return}
let currentPageNumber = Int(pageNumber) % count
pageControl.currentPage = currentPageNumber
}
以上是关于UIPageControl的无限UICollectionview的主要内容,如果未能解决你的问题,请参考以下文章