ios - Getting the screen location of a cell from a UICollectionView -
this isn't much question explanation of how solve problem.
the first thing realize uicollectionview
inherit uiscrollview
- doing standard lookup scroll view's content best solution.
here's problem addressing:
i had uicollectionview
had differing items in each cell - along differing types of cells. need selection of cell cause effect of image in cell appear expand , take on whole screen. how did expansion post.
the challenge getting cell's position on screen animating section have reference point start.
so, facilitate getting info - consider next code:
first note:
uicollectionview *picturescollectionview; drawingcell cell; // -> instanceof uicollectionviewcell custom items. // first, list of cells visible on screen - must every time // since items can change... critical fact. not go through // entire list of cells - collectionview indicates visible. note // there things watch out - visibles array not match indexpath.item // number - independent. latter item number overall cells, while // visibles array may have 2 entries - there not 1-to-1 mapping - maintain // in mind. nsarray *visibles = [self.picturescollectionview visiblecells]; // now, cycle through times , find 1 matches criteria. in // case, check cell indexpath passed matches cell's imageview... // indexpath passed in method phone call - note indexpath point // number in datasource particular item - crucial. (int i=0; i<visibles.count; i++) { drawingcell *cell = (drawingcell *)visibles[i]; if (cell.imageview.image == (uiimage *)images[indexpath.item]) { // @ point, we've found right cell - translation determine // it's location on current screen... getting contentoffset // collectionview subtracted cell's origin - , adding in (in case) // frame offset position of item wish animate (in case // imageview contained within custom collection cell... cgfloat relativex = cell.frame.origin.x - self.picturescollectionview.contentoffset.x + cell.imageview.frame.origin.x; cgfloat relativey = cell.frame.origin.y - self.picturescollectionview.contentoffset.y + cell.imageview.frame.origin.y; // have exact screen coordinates of imageview - since need // perform animations, save off in cgrect - in case, set size // size of imageview - doing flicker display // wanted grow selected image, coordinates of image // in cell , size displayed image... uiimageview *image = cell.imageview; // selectedcell cgrect that's global sake of code... selectedcell = cell.frame; 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. have coordinates , size of imageview wish animate , grow...
hopefully, helps other folks trying figure out how overlay on cell in 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]]; nslog(@"%f",cellframeinsuperview.origin.x); }
it work me.you can seek yourself
ios uicollectionview uicollectionviewcell
No comments:
Post a Comment