【问题标题】:Peek/Pop preview ignores cell corner radius in collection viewPeek/Pop 预览忽略集合视图中的单元格角半径
【发布时间】:2016-05-28 01:04:06
【问题描述】:

我在我的集​​合视图单元格中添加了 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
}

有人有什么建议吗?

【问题讨论】:

  • 嗨,你能把你的测试项目上传到某个地方吗?谢谢

标签: ios swift uicollectionview 3dtouch peek-pop


【解决方案1】:

据我了解,您希望获得类似第一的东西,而不是第二:

问题是您在整个视图上注册通知。像这样的东西: 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 :)

【讨论】:

  • 你能解释一下previwingControllers是什么吗?
  • @Viper 嗨!它是registerForPreviewingWithDelegate(: sourceView:)返回的类的对象,因为注销方法(unregisterForPreviewingWithContext(:))把它作为参数,所以你应该把它们存储在某个地方。
猜你喜欢
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
相关资源
最近更新 更多