简体   繁体   English

ios - Peek和Pop - 将UIImageView的角半径保持在UITableViewCell内的UICollectionView中

[英]ios - Peek and Pop - keep corner radius of UIImageView inside UICollectionView inside UITableViewCell

My view is structured like this: 我的观点结构如下:

UITableView
|    
+-- MyTableViewCell
|  |  
|  +-- UICollectionView
|     |  
|     +-- MyCollectionViewCell
|       |  
|       +-- UIImageView

The UIImageView is named 'icon' and its layer has certain corner radius. UIImageView被命名为“icon”,其图层具有一定的角半径。 I've implemented Peek and Pop for items inside the Collection View and it works, but I can't figure out how to include corner radius of the image in the preview frame. 我已经为集合视图中的项目实现了Peek和Pop,它可以工作,但我无法弄清楚如何在预览框架中包含图像的角半径。

This is how I register the tableView for previewing: 这是我如何注册tableView进行预览:

if #available(iOS 9.0, *), traitCollection.forceTouchCapability == .available {
    registerForPreviewing(with: self, sourceView: tableView)
}

And this is my previewing function (inspired from here ): 这是我的预览功能(灵感来自这里 ):

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {

    guard let indexPath = tableView.indexPathForRow(at: location) else { return nil }
    guard let cell = tableView.cellForRow(at: indexPath) as? MyTableViewCell else { return nil }

    guard let collectionIndex = cell.collectionView.indexPathForItem(at: self.view.convert(location, to: cell.collectionView)) else { return nil }

    guard let collectionViewCell = cell.collectionView.cellForItem(at: collectionIndex) as? MyCollectionViewCell else { return nil }
    let iconRect = tableView.convert(collectionViewCell.icon.frame, from: collectionViewCell.icon.superview!)
    previewingContext.sourceRect = iconRect

    return someViewController

}

This is what I get in the preview frame when force touching the image, right before the new view controller is committed (so the outside is blurred): 这是我在提交新视图控制器之前强制触摸图像时在预览框中得到的内容(因此外部模糊):

此搜索

This is what I would like to achieve, corner radius of the image is preserved (screenshot from the App Store): 这就是我想要实现的,保留图像的圆角半径(来自App Store的截图):

图像2

By reading other posts I believe the issue is that I register the whole tableView for previewing, instead of its cells. 通过阅读其他帖子我相信问题是我注册了整个tableView进行预览,而不是它的单元格。 However I could not find a solution that worked for Collection View cells inside Table View cells. 但是我找不到适用于Table View单元格内的Collection View单元格的解决方案。

Any help appreciated. 任何帮助赞赏。

你能覆盖UIImageView并创建某种自定义绘图功能吗?

Instead of converting the frame, did you try using its cellAttributes? 您是否尝试使用其cellAttributes而不是转换框架? If there is a cornerRadius on the contentView.layer of the cell, this will keep that in mind. 如果单元格的contentView.layer上有cornerRadius,这将记住这一点。 So something like this works for us: 所以这样的事情对我们有用:

let cellAttributes = collectionView.layoutAttributesForItem(at: indexPath)

previewingContext.sourceRect = cellAttributes.frame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM