预览/弹出预览在集合视图中忽略单元角半径 [英] Peek/Pop preview ignores cell corner radius in collection view

查看:50
本文介绍了预览/弹出预览在集合视图中忽略单元角半径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我向收藏夹视图单元中添加了3D Touch Peek/Pop功能,效果很好,但是我注意到预览框不遵守单元的角半径.

这是我的预览功能:

  func PreviewingContext(previewingContext:UIViewControllerPreviewing,viewControllerForLocation位置:CGPoint)->UIViewController?{让viewController =情节提要吗?instantiateViewControllerWithIdentifier("scholarDetailViewController")为?ScholarDetailViewController让cellPosition = self.scholarsCollectionView.convertPoint(位置,fromView:self.view)让cellIndex = self.scholarsCollectionView.indexPathForItemAtPoint(cellPosition)保护我们,让PreviewViewController = viewController,indexPath = cellIndex,单元格= self.scholarsCollectionView.cellForItemAtIndexPath(indexPath)else {返回零}让学者= self.searchBarActive?self.searchResults [indexPath.item]为!学者:self.currentScholars [indexPath.item]PreviewViewController.setScholar(scholar.id)PreviewViewController.delegate =自我PreviewViewController.preferredContentSize = CGSize.zeroPreviewingContext.sourceRect = self.view.convertRect(cell.frame,fromView:self.scholarsCollectionView)返回previewViewController} 

我尝试设置PreviewingContext sourceView的拐角半径并在单元格上使用masksToBounds进行播放,但是到目前为止我没有尝试过.

这是单元格设置:

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

有人有建议吗?

解决方案

据我所知,您希望拥有类似第一种的东西,而不是第二种:

问题是您要在整个视图上注册通知.像这样的东西: registerForPreviewingWithDelegate(self,sourceView:self.view),这就是为什么您触摸的区域对单元层一无所知.

您应该做的-亲自注册每个单元:

  func collectionView(collectionView:UICollectionView,willDisplayCell单元格:UICollectionViewCell,forItemAtIndexPath indexPath:NSIndexPath){让previwingController = registerForPreviewingWithDelegate(self,sourceView:cell)previwingControllers [cell] = previwingController}func collectionView(collectionView:UICollectionView,didEndDisplayingCell单元格:UICollectionViewCell,forItemAtIndexPath indexPath:NSIndexPath){如果让previwingController = previwingControllers [cell] {unregisterForPreviewingWithContext(previwingController)}} 

然后只需将 previewingContext.sourceRect = self.view.convertRect(cell.frame,fromView:self.scholarsCollectionView)更改为 previewingContext.sourceRect = cell.bounds

P.S.当然,不要忘记在视图中删除 registerForPreviewingWithDelegate :)

I have added 3D Touch Peek/Pop functionality to my collection view cells and it works great, however I've noticed that the preview frame does not respect the corner radius of the cells.

Here's my previewing function:

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
}

I've tried setting the corner radius of the previewingContext sourceView and playing around with masksToBounds on the cell, but nothing I've tried so far has helped.

Here's the cell setup:

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

Anyone have any suggestions?

解决方案

As I understand you correctly you want to have something like first, instead of second:

Problem is that you register notification on whole view. Something like this: registerForPreviewingWithDelegate(self, sourceView: self.view), that why your touched area know nothing about cell layer.

What you should do — register every cell personally:

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)
    }
}

And just change previewingContext.sourceRect = self.view.convertRect(cell.frame, fromView: self.scholarsCollectionView) to previewingContext.sourceRect = cell.bounds

P.S. Of course don't forget to remove registerForPreviewingWithDelegate on your view :)

这篇关于预览/弹出预览在集合视图中忽略单元角半径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆