Swift How to get the centre cell in a collectionView and resize it when scrolling?

后端 未结 1 1653
南旧
南旧 2021-01-13 14:48

I have a horizontal collectionView and i would like to manage that when scrolling it, the centre cell becomes larger. So, each time a cell is the center cell it will be larg

相关标签:
1条回答
  • 2021-01-13 15:33

    Try this out:

    func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,
    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if collectionView == verticalCollectionView {
    let size = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
            return size
        }
    let point = CGPoint(x: self.view.frame.size.width/2.0, y:35 )
    //or you can try this code also
    //let point = CGPoint(x: UIScreen.main.bounds.width/2, y:35 )
        print(point)
    let centerIndex = self.collectionView.indexPathForItemAtPoint(point)
        if indexPath.item == centerIndex {
            return CGSize(width: 70, height: 70)
        }
        return CGSize(width: 50, height: 50)
    }
    

    This code gives you the index of the item at the center of the collection View. Hope this helps

    0 讨论(0)
提交回复
热议问题