我已将 3D Touch 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 的角半径并在单元格上使用 maskToBounds ,但到目前为止我尝试过的任何方法都没有帮助。
这是单元格设置:
override func awakeFromNib() {
self.layer.cornerRadius = 7
}
有人有什么建议吗?
据我正确理解,您想要第一个而不是第二个:
问题是您在整个视图上注册通知。像这样的事情: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)
to previewingContext.sourceRect = cell.bounds
附:当然不要忘记删除registerForPreviewingWithDelegate
按照你的看法:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)