layoutAttributesForItemAtIndexPath 不 return 执行 peek 和 pop 时的实际位置
layoutAttributesForItemAtIndexPath doesn't return the actual position when performing peek and pop
我正在尝试使用以下代码实现 Peek 和 Pop 以预览照片库中的图像:
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
}
虽然可以,但问题是滚动集合视图后,预览区域在尝试查看时有时不在准确的单元格位置。
有什么方法可以获得单元格的实际位置?
我找到了这个问题的解决方案,原来它与registerForPreviewingWithDelegate有关。
而不是
registerForPreviewingWithDelegate(self, sourceView: view)
应该是
registerForPreviewingWithDelegate(self, sourceView: self.collectionView!)
我正在尝试使用以下代码实现 Peek 和 Pop 以预览照片库中的图像:
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
}
虽然可以,但问题是滚动集合视图后,预览区域在尝试查看时有时不在准确的单元格位置。
有什么方法可以获得单元格的实际位置?
我找到了这个问题的解决方案,原来它与registerForPreviewingWithDelegate有关。
而不是
registerForPreviewingWithDelegate(self, sourceView: view)
应该是
registerForPreviewingWithDelegate(self, sourceView: self.collectionView!)