Это не столько вопрос, сколько объяснение того, как решить эту проблему.
Первое, что нужно понять, - это то, что UICollectionView
наследует от UIScrollView
, поэтому наилучшим решением является стандартный поиск со списком прокрутки.
Здесь проблема, с которой я обращался:
У меня был UICollectionView
, у которого были разные элементы в каждой ячейке - вместе с различными типами ячеек. Мне нужен выбор ячейки, чтобы вызвать эффект изображения в ячейке, чтобы он расширялся и захватил весь экран. Как я сделал расширение для другой должности.
Задача заключалась в том, чтобы получить положение ячейки на экране, чтобы анимационная секция имела контрольную точку от того, где начать.
Итак, чтобы облегчить получение этой информации, рассмотрите следующий код:
Первое примечание:
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 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 the translation to determine
// what is it location on the current screen... You do this by getting the contentOffset
// from the collectionView subtracted from the cell origin - and adding in (in my case)
// the frame offset for the position of the item I wish to animate (in my case the
// imageView contained within my 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;
// I now have the exact screen coordinates of the imageView - so since I need this
// to perform animations, I save it off in a CGRect - in my case, I set the size
// exactly to the size of the imageView - so say you were doing a Flicker display
// where you wanted to grow a selected image, you get the coordinates of the image
// in the cell and the size from the displayed image...
UIImageView *image = cell.imageView;
// selectedCell is a CGRect that global for the sake of this 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. I have my coordinates and the size of the imageView I wish to animate and grow...
Надеюсь, это поможет другим людям, которые пытаются понять, как сказать, что-то накладывать на ячейку в точном месте и т.д.