Skip to content

Instantly share code, notes, and snippets.

@ajself
Created April 5, 2017 14:17
Show Gist options
  • Save ajself/f9cdc3d16b68491e0def453a50d442f9 to your computer and use it in GitHub Desktop.
Save ajself/f9cdc3d16b68491e0def453a50d442f9 to your computer and use it in GitHub Desktop.
Get location of cell for UICollectionView previewing - peek/pop
// I was getting bad results for collectionView.indexPathForItem(at: location)
// Ends up the scroll view content offset needs to be taken into consideration.
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let collectionView = collectionView else { return nil }
// The magic sauce
let cellLocation = CGPoint(x: location.x + collectionView.contentOffset.x,
y: location.y + collectionView.contentOffset.y)
guard let indexPath = collectionView.indexPathForItem(at: cellLocation) else { return nil }
guard let cell = collectionView.cellForItem(at: indexPath) else { return nil }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment