2016-05-28 28 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(:)')把它作为参数,所以你应该将它们存储在某个地方。 –