Peek / Pop预览忽略集合视图中的单元格角半径

时间:2016-05-28 01:04:07

标签: ios swift uicollectionview 3dtouch peek-pop

我已将3D Touch Peek / Pop功能添加到我的集合视图单元格中并且效果很好,但我注意到预览框架不尊重单元格的角半径。

这是我的预览功能:

public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

我已经尝试设置previewingContext sourceView的角半径并在单元格中使用masksToBounds,但到目前为止我没有尝试过任何帮助。

这是细胞设置:

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
}

有人有任何建议吗?

1 个答案:

答案 0 :(得分:9)

正如我理解你的那样,你想要的是第一个而不是第二个:

Right one Wrong 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

即可

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