how to get indexPath for cell which is located in the center of UICollectionView

前端 未结 11 2194
慢半拍i
慢半拍i 2020-12-25 09:41

How can i find indexPath for cell in the middle of UICollectionView?

I have horizontal scrolling and only one big cell<

11条回答
  •  一生所求
    2020-12-25 10:36

    I made like for horizontal UICollectionView I use Swift 2.x.

    private func findCenterIndex() {
        let collectionOrigin = collectionView!.bounds.origin
        let collectionWidth = collectionView!.bounds.width
        var centerPoint: CGPoint!
        var newX: CGFloat!
        if collectionOrigin.x > 0 {
            newX = collectionOrigin.x + collectionWidth / 2
            centerPoint = CGPoint(x: newX, y: collectionOrigin.y)
        } else {
            newX = collectionWidth / 2
            centerPoint = CGPoint(x: newX, y: collectionOrigin.y)
        }
    
        let index = collectionView!.indexPathForItemAtPoint(centerPoint)
        print(index)
    }
    
     override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        findCenterIndex()
    }
    

提交回复
热议问题