gpt4 book ai didi

ios - UICollectionView 的 UICollectionReusableView 防止/阻止对 UICollectionViewCell 的点击

转载 作者:行者123 更新时间:2023-11-28 15:38:38 31 4
gpt4 key购买 nike

我正在使用带有流布局的 UICollectionView。我已经设置了 header ,它是一个 UICollectionReusableView,其行为如下;

layout?.sectionHeadersPinToVisibleBounds = true

...

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath as IndexPath)

header.layer.zPosition = -1
return header
}

这提供了预期的效果,当向上滚动单元格时,标题保持固定但位于常规单元格后面。

但是,如果我尝试单击向顶部滚动的 UICollectionViewCell,即它在技术上覆盖了 UICollectionReusableView,则 UICollectionViewCell 的 didSelectItemAt tap 事件将不再触发,直到我将它向下滚动到远离标题所在的位置。换句话说,UICollectionReusableView 正在阻止点击手势,即使它的 zPosition 设置为 -1 并且不可见。

UICollectionView zIndex issue explained

有没有人遇到过这个问题,你是如何解决的?

最佳答案

将此添加到您的 Section Header View 类:

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return false
}

会将所有触摸传递给下面的下一个接收器 - 在本例中为您的 Collection View 单元格。如果您需要在 Section Header 中使用交互式元素(例如按钮),您可以执行以下操作:

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
let ptInSub = theButton.convert(point, from: theButton.superview)
if theButton.bounds.contains(ptInSub) {
return true
}
return false
}

可以给你你想要的东西,虽然......如果单元格 View 覆盖了部分标题上的按钮,并且你点击按钮所在的单元格,按钮将采用轻敲。应该能够通过另一个 contains(point) 或两个...

来解决这个问题

关于ios - UICollectionView 的 UICollectionReusableView 防止/阻止对 UICollectionViewCell 的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44006554/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com