【发布时间】:2018-02-26 18:51:43
【问题描述】:
在下面给定的代码中,我可以简单地无限滚动单元格。页面控件似乎至少在向前滚动(屏幕右侧)时也可以工作。但坦率地说,当我向左滚动时,我看到了 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)
}
}
}
【问题讨论】:
-
你能减少你的代码只显示与问题相关的位吗?就像您设置页面控件值一样。有太多代码需要过滤。
-
使用 FSPagerView pod
标签: swift uicollectionview swift4 infinite-scroll uipagecontrol