Created
April 5, 2017 14:17
-
-
Save ajself/f9cdc3d16b68491e0def453a50d442f9 to your computer and use it in GitHub Desktop.
Get location of cell for UICollectionView previewing - peek/pop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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