【问题标题】:ios - Peek and Pop - keep corner radius of UIImageView inside UICollectionView inside UITableViewCellios - Peek and Pop - 在 UITableViewCell 内的 UICollectionView 内保持 UIImageView 的角半径
【发布时间】:2017-07-12 22:54:58
【问题描述】:

我的视图结构如下:

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

UIImageView 被命名为'icon',它的图层有一定的圆角半径。 我已经为 Collection View 中的项目实现了 Peek 和 Pop 并且它可以工作,但我无法弄清楚如何在预览框架中包含图像的角半径。

这就是我注册 tableView 以进行预览的方式:

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

这是我的预览功能(灵感来自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

}

这是我在强制触摸图像时在预览帧中得到的,就在提交新视图控制器之前(所以外面是模糊的):

这是我想要实现的,图像的角半径被保留(来自 App Store 的屏幕截图):

通过阅读other posts,我认为问题在于我注册了整个 tableView 以进行预览,而不是其单元格。但是,我找不到适用于表格视图单元格中的集合视图单元格的解决方案。

任何帮助表示赞赏。

【问题讨论】:

    标签: ios swift uitableview 3dtouch peek-pop


    【解决方案1】:

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

    【讨论】:

    • 您能详细说明一下吗?
    • 可能是这样的:link。这不是我说的,但您绝对可以访问这些对象并对其进行自定义。
    • 图像已经设置了圆角半径并且工作正常。问题在于预览框架中的 Peek and Pop,当将图标 rect 传递给 previewingContext.sourceRect 时,我无法弄清楚如何保持圆角半径
    • 啊,是的,你确实这么说过。我很抱歉。如何在 IB 中将 UIImageView 和/或父视图的背景设置为透明?
    【解决方案2】:

    您是否尝试使用它的 cellAttributes 而不是转换框架?如果单元格的 contentView.layer 上有cornerRadius,请记住这一点。所以这样的事情对我们有用:

    let cellAttributes = collectionView.layoutAttributesForItem(at: indexPath)
    
    previewingContext.sourceRect = cellAttributes.frame
    

    【讨论】:

    • 感谢您的回答。 cornerRadius 实际上只设置在我的 imageView 的层上,而不是在 contentView 的层上,所以我认为这行不通。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多