Obtain the screen location of a cell from a UICollection view

This isn’t so much a question as an explanation of how to solve this problem.

The first thing to realize is that the UICollectionView
does inherit from a UIScrollView
– so doing a standard lookup with a scroll view’s content is the best solution.

Here’s the problem I was addressing:

I had a UICollectionView
that had differing items in each cell – along with differing types of cells. I need the selection of a cell to cause an effect of the image in the cell to appear to expand and take over the whole screen. How I did the expansion is for another post.

The challenge was getting the cell’s position on the screen so that the animating section would have a reference point from where to start.

So, to facilitate getting this information – consider the following code:

First note:

UICollectionView *picturesCollectionView;
DrawingCell cell; // -> instanceof UICollectionViewCell with custom items.

// first, get the list of cells that are visible on the screen - you must do this every time
// since the items can change...  This is a CRITICAL fact.  You do not go through the
// entire list of cells - only those the collectionView indicates are visible.  Note
// there are some things to watch out for - the visibles array does not match the indexPath.item
// number - they are independent.  The latter is the item number overall the cells, while
// the visibles array may have only 2 entries - so there is NOT a 1-to-1 mapping - keep
// that in mind.

NSArray *visibles = [self.picturesCollectionView visibleCells];

// now, cycle through the times and find the one that matches some criteria.  In my
// case, check that the cell for the indexPath passed matches the cell's imageView...
// The indexPath was passed in for the method call - note that the indexPath will point
// to the number in your datasource for the particular item - this is crucial.

for (int i=0; i<visibles.count; i++)="" {="" drawingcell="" *cell="(DrawingCell" *)visibles[i];="" if="" (cell.imageview.image="=" (uiimage="" *)images[indexpath.item])="" at="" this="" point,="" we've="" found="" the="" correct="" cell="" -="" now="" do="" translation="" to="" determine="" what="" is="" it's="" location="" on="" current="" screen...="" you="" by="" getting="" contentoffset="" from="" collectionview="" subtracted="" cell's="" origin="" and="" adding="" in="" (in="" my="" case)="" frame="" offset="" for="" position="" of="" item="" i="" wish="" animate="" case="" imageview="" contained="" within="" custom="" collection="" cell...="" cgfloat="" relativex="cell.frame.origin.x" self.picturescollectionview.contentoffset.x="" +="" cell.imageview.frame.origin.x;="" relativey="cell.frame.origin.y" self.picturescollectionview.contentoffset.y="" cell.imageview.frame.origin.y;="" have="" exact="" screen="" coordinates="" so="" since="" need="" perform="" animations,="" save="" it="" off="" a="" cgrect="" case,="" set="" size="" exactly="" say="" were="" doing="" flicker="" display="" where="" wanted="" grow="" selected image,="" get="" image="" displayed="" image...="" uiimageview="" *image="cell.imageView;" selectedcell="" that's="" global="" sake="" code...="" selectedcell.origin.x="relativeX;" selectedcell.origin.y="relativeY;" selectedcell.size.width="cell.imageView.frame.size.width;" selectedcell.size.height="cell.imageView.frame.size.height;" }="" done.="" grow...

Hopefully, this helps other folks that are trying to figure out how to say overlay something on the cell in an exact position, etc...

-(void)collectionView:(UICollectionView *)cv didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

UICollectionViewLayoutAttributes *attributes = [cv layoutAttributesForItemAtIndexPath:indexPath];

CGRect cellRect = attributes.frame;

CGRect cellFrameInSuperview = [cv convertRect:cellRect toView:[cv superview]];


It work for me.You can try yourself

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Obtain the screen location of a cell from a UICollection view

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录