Expanding UICollectionView and its cell when tapped

℡╲_俬逩灬. 提交于 2019-12-03 20:46:44

You cannot just use didSelectItemAtIndexPath or any similar methods to update the size of a UICollectionViewCell once the UICollectionView is done performing the view layout.

To update cell height, You can first capture which cell had been tapped in didSelectItemAtIndexPath. Then, you can either reload the entire collection view with the new cell frame being passed in the sizeForItemAtIndexpath. Or, you can just reload the specific cell with reloadItemsAtIndexPaths, but you still need to pass the updated size of the cell via sizeForItemAtIndexpath.

UPDATE I now see the question details have been updated by an animation which you desire to have.

I had performed a similar animation by:-

  1. Capturing the cell which had been tapped in didSelectItemAtIndexPath.
  2. Adding a replica view to the UIViewContrller, but with its frame totally coinciding with the cell which had been tapped.
  3. Then, animating this view which had been added. Thus giving an impression that the cell was animated.

Any additional functionality which has to be given can also be written in this view. Thus the code of the cell and the animated view is separated too.

Or what you can do is expand the item and change its frame with UIAnimation.

And when he cell is tapped, you get the views inside the cell to be expanded also using auto layout and I'm hinting towards (clips to bounds).

something like this:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    let item = collectionView.cellForItemAtIndexPath(indexPath) as! cellCollectionViewExpress  // or whatever you collection view cell class name is.

    UIView.animateWithDuration(1.0, animations: {
        self.view.bringSubviewToFront(collectionView)
        collectionView.bringSubviewToFront(item)
        item.frame.origin = self.view.frame.origin   /// this view origin will be at the top of the scroll content, you'll have to figure this out
        item.frame.size.width = self.view.frame.width
        item.frame.size.height = self.view.frame.height
    })
}

I would suggest you that you use UICollectionView Controller, so things are at ease in general with using that.

When a cell tapped or a button or any tappable thing got tapped inside the cell, then you get the call from didSelectItemAtIndexPath or through delegate, then to give the cell the required size, you have to invalidate the layout of the current collectionview. After this, size for item will get called and give the new size for the,

This will update the size of the collectioncell without reloading it. You can give animation also.

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
        flowLayout.invalidateLayout()
    }

}

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