2016-05-28 17 views
2

我已將3D Touch Peek/Pop功能添加到我的集合視圖單元中並且效果很好,但是我注意到預覽框架並不尊重圓角的半徑細胞。Peek/Pop預覽忽略集合視圖中的單元圓角半徑

這是我的預覽功能:

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? { 
    let viewController = storyboard?.instantiateViewControllerWithIdentifier("scholarDetailViewController") as? ScholarDetailViewController 
    let cellPosition = self.scholarsCollectionView.convertPoint(location, fromView: self.view) 
    let cellIndex = self.scholarsCollectionView.indexPathForItemAtPoint(cellPosition) 

    guard let previewViewController = viewController, indexPath = cellIndex, cell = self.scholarsCollectionView.cellForItemAtIndexPath(indexPath) else { 
     return nil 
    } 

    let scholar = self.searchBarActive ? self.searchResults[indexPath.item] as! Scholar : self.currentScholars[indexPath.item] 
    previewViewController.setScholar(scholar.id) 
    previewViewController.delegate = self 
    previewViewController.preferredContentSize = CGSize.zero 
    previewingContext.sourceRect = self.view.convertRect(cell.frame, fromView: self.scholarsCollectionView) 

    return previewViewController 
} 

我已經嘗試設置previewingContext sourceView的圓角半徑,並與細胞masksToBounds玩耍,但沒有到目前爲止,我已經試過了幫助。

這裏的小區建立:

override func awakeFromNib() { 
    self.layer.cornerRadius = 7 
} 

任何人有什麼建議?

+0

嗨,你可以上傳你的測試項目的地方?謝謝 –

回答

7

正如我理解正確的話,你想擁有像第一,而不是第二:

Right oneWrong one

問題是,你註冊的全視圖通知。就像這樣: registerForPreviewingWithDelegate(self, sourceView: self.view),那爲什麼你碰到的區域對細胞層一無所知。

你應該做的 - 個人註冊的每一個細胞:

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 

    let previwingController = registerForPreviewingWithDelegate(self, sourceView: cell) 
    previwingControllers[cell] = previwingController 
} 

func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { 

    if let previwingController = previwingControllers[cell] { 
     unregisterForPreviewingWithContext(previwingController) 
    } 
} 

而只是改變previewingContext.sourceRect = self.view.convertRect(cell.frame, fromView: self.scholarsCollectionView)previewingContext.sourceRect = cell.bounds

附:當然,不要忘記刪除registerForPreviewingWithDelegate你的看法:)

+0

你能解釋一下previwingControllers是什麼嗎? – Viper

+1

@Viper嗨!它是由'registerForPreviewingWithDelegate(:sourceView:)'返回的類的對象,因爲unregister方法('unregisterForPreviewingWithContext(:)')把它作爲參數,所以你應該將它們存儲在某個地方。 –