I am trying to implement Peek and Pop to preview the image on photo library with the following code:
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
if let indexPath = self.collectionView!.indexPathForItemAtPoint(location), cellAttributes = self.collectionView!.layoutAttributesForItemAtIndexPath(indexPath) {
previewingContext.sourceRect = cellAttributes.frame
}
let selectedPhotoAsset = self.allPhotos[self.selectedPhotoIndexPath![0].item] as! PHAsset
guard let displayImageDetailVC = storyboard?.instantiateViewControllerWithIdentifier("DisplayImageDetailVC") as? DisplayImageViewController else {
print("Error instantiate DisplayImageDetail View Controller")
return nil
}
displayImageDetailVC.selectedPhotoAsset = selectedPhotoAsset
return displayImageDetailVC
}
Even though it works, but the problem is after the collection view scrolled, the preview area sometimes is not at the exact cell's position when trying to peek.
Is there any way that I can get the actual position of the cell?
I have found the solution for this issue, turns out it is related to registerForPreviewingWithDelegate.
Instead of
registerForPreviewingWithDelegate(self, sourceView: view)
It should be
registerForPreviewingWithDelegate(self, sourceView: self.collectionView!)