Collectionview 结束拖动位置不要停在每个单元格上

Collectionview the end Draging Position dont stop on each cell

有人可以帮助我吗? 我有带插图的集合视图、水平滚动方向、启用分页并且单元格之间没有 space。问题是寻呼不会在每个单元格上停止。

我尝试了在 Whosebug 和万维网上可以找到的所有可能的解决方案,但没有任何效果。我使用 Xcode 10 和 Swift5,这是针对 iPhone 应用程序的。我没有添加我的 collectionviewcell 代码,因为它只有标签的 IBoutlet。

 class ViewController: UIViewController, UICollectionViewDataSource, 
     UICollectionViewDelegate {

 @IBOutlet weak var collectionView: UICollectionView!

    private let item = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"]
    private let cellScaling: CGFloat = 0.33
    private var cellWidth: CGFloat = 0


    override func viewDidLoad() {
        super.viewDidLoad()

        UpdateUI()
    }

    func UpdateUI(){
        self.collectionView.backgroundColor = UIColor.white

        let screenSize = UIScreen.main.bounds.size
        let cellWidth = floor(screenSize.width * cellScaling)
        let insetX = (view.bounds.width - cellWidth) / 2
        let layout = collectionView!.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: cellWidth, height: collectionView.bounds.height)

        collectionView.contentInset = UIEdgeInsets(top: 0, left: insetX, bottom: 0, right: insetX)

     // layout.minimumLineSpacing = 0
        self.cellWidth = cellWidth
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.item.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

        cell.label.text = self.item[indexPath.row]
        cell.backgroundColor = UIColor.cyan

        return cell
    }

    private var startingScrollingOffset = CGPoint.zero

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startingScrollingOffset = scrollView.contentOffset
    }

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        let page: CGFloat
        let offset = scrollView.contentOffset.x + scrollView.contentInset.left
        let proposedPage = offset / max(1, cellWidth)
        let snapPoint: CGFloat = 0.1
        let snapDelta: CGFloat = offset > startingScrollingOffset.x ? (1-snapPoint) : snapPoint

        if floor(proposedPage + snapDelta) == floor(proposedPage){
            page = floor(proposedPage)
        } else {
            page = floor(proposedPage + 1)
        }

        targetContentOffset.pointee = CGPoint(x: cellWidth * page, y: targetContentOffset.pointee.y)

    }
}

enter image description here

请注意,我尝试了我能找到的所有其他解决方案。在您投票删除此问题之前。照片显示了它在每个单元格上的样子。

几个月前我不得不做一个类似的集合视图, 这是我使用的代码:

   func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        let layout = theNameOfYourCollectionView.collectionViewLayout as! UICollectionViewFlowLayout 
        let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing // Calculate cell size
        let offset = scrollView.contentOffset.x
        let index = (offset + scrollView.contentInset.left) / cellWidthIncludingSpacing // Calculate the cell need to be center

        if velocity.x > 0 { // Scroll to -->
            targetContentOffset.pointee = CGPoint(x: ceil(index) * cellWidthIncludingSpacing - scrollView.contentInset.right, y: -scrollView.contentInset.top)
        } else if velocity.x < 0 { // Scroll to <---
            targetContentOffset.pointee = CGPoint(x: floor(index) * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
        } else if velocity.x == 0 { // No dragging
            targetContentOffset.pointee = CGPoint(x: round(index) * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
        }
    }

此代码计算单元格的大小、已显示的单元格数量以及滚动完成后,调整它以使单元格居中。

如果要使用此代码,请确保 false 中有 collectionView 的 pagingEnabled

希望对你有所帮助!