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

三世轮回 提交于 2019-12-01 09:03:58

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!