UICollectionView horizontal paging with 3 items

前端 未结 6 1304
眼角桃花
眼角桃花 2020-12-04 06:21

I need to show 3 items in a UICollectionView, with paging enabled like this

\"enter

6条回答
  •  庸人自扰
    2020-12-04 06:37

    Mine solution to horizontal collection view paging

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == collectionView { collectionView.scrollToPage() }
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if scrollView == collectionView { if !decelerate { collectionView.scrollToPage() } }
    }
    

    And small extension to collectionView

    public func scrollToPage() {
            var currentCellOffset = contentOffset
            currentCellOffset.x += width / 2
            var path = indexPathForItem(at: currentCellOffset)
            if path.isNil {
                currentCellOffset.x += 15
                path = indexPathForItem(at: currentCellOffset)
            }
            if path != nil {
                logInfo("Scrolling to page \(path!)")
                scrollToItem(at: path!, at: .centeredHorizontally, animated: true)
            }
        }
    

提交回复
热议问题