What is the correct syntax for using UICollectionView's indexPathForItemAtPoint?
I'm using the following code to try to identify the cell that is at the centre of the UICollectionView as I scroll through it.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//locate the scrollview which is in the centre
CGPoint centerPoint = CGPointMake(self.collectionView.frame.size.width / 2, self.collectionView.frame.size.height /2);
NSIndexPath *indexPathOfCentralCell = [self.collectionView indexPathForItemAtPoint:centerPoint];
}
However, this always gives me the same indexPath back, no matter how I scroll.
What am I doing wrong?
Here is how to do it in Swift if you want to retrieve a specific UICollectionViewCell with a UILongPressGesture
Duncan, this is what it works for me in Swift:
You have to keep in mind the scrolling view of collection view.
An example of where you'd want to do this is when you want to capture a gesture on the UICollectionView, and then do something with the specific item in the UICollectionView that falls under the location of the swipe gesture. The code below as a method in your UICollectionViewController will return the correct cell, and accounts for scrolling within the Collection View.
You might call the above method in a context like this (where
revealSupplementalControls
is a custom method on your UICollectionViewCell class):You are looking up a static point in a static field of cells. Yes you will always get the same cell.
In order to deal with scrolling you need to add the scrollview's position into your check. Something like this: