2016-02-13 51 views
0

我想實現皮克和流行預覽的照片庫下面的代碼的圖像:layoutAttributesForItemAtIndexPath不返回的實際位置,並彈出

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 
} 

即使它的工作原理,但問題出現在收集視圖滾動後,預覽區域有時在試圖查看時不在準確的單元格位置。 enter image description here

有沒有什麼辦法可以得到細胞的實際位置?

回答

0

我發現這個問題的解決方案,原來它與registerForPreviewingWithDelegate有關。

而不是

registerForPreviewingWithDelegate(self, sourceView: view) 

應該

registerForPreviewingWithDelegate(self, sourceView: self.collectionView!)