UICollectionView Horizontal Paging not centered

前端 未结 11 1664
感动是毒
感动是毒 2020-12-02 09:32

I have a horizontal scrolling collectionView with each cell the size of the view. When I page through the collectionView it doesn\'t page by cell. The cells aren\'t in the c

11条回答
  •  Happy的楠姐
    2020-12-02 09:52

    Swift 3

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
            if scrollView == self.collectionView {
                var currentCellOffset = self.collectionView.contentOffset
                currentCellOffset.x += self.collectionView.frame.width / 2
                if let indexPath = self.collectionView.indexPathForItem(at: currentCellOffset) {
                  self.collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
                }
            }
        }
    

提交回复
热议问题