gpt4 book ai didi

ios - 回调到 sectionHeadersPinToVisibleBounds

转载 作者:行者123 更新时间:2023-11-28 21:09:55 26 4
gpt4 key购买 nike

我在我的项目中有一个要求,当 UICollectionView header 变粘时,我必须向它们添加阴影。

我正在使用 UICollectionViewFlowLayout sectionHeadersPinToVisibleBounds 上的属性,当设置为 YES 时,它会使标题变粘。

当标题变得粘滞并实际固定到顶部时,我们能否获得某种回调,以便我可以为该标题添加阴影?当标题不粘时,我不希望阴影在那里。

这里的挑战是,我无法真正利用方法 layoutAttributesForSupplementaryView: 中的信息,它确实为我提供了屏幕上的 View ,因为我的部分具有动态的项目数,因此屏幕上的部分并不真正告诉我谁需要影子。

最佳答案

我设法通过以下方式找到了这个问题的解决方案:

  1. 手动保存标题位置:

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    let cell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CategoryNameReusableView", for: indexPath) as! CategoryNameReusableView
    cell.nameLbl.text = sectionNames[indexPath.section]
    if viewWithPosition.isEmpty { viewWithPosition.append(( name: cell.nameLbl.text!, position: cell.frame.origin.y))}
    let x = isElementInArray(array: viewWithPosition, string: cell.nameLbl.text!)
    if !x {
    viewWithPosition.append((name: cell.nameLbl.text!, position: cell.frame.origin.y))
    }
    return cell
    }

我正在使用 viewWithPosition 元组数组 var viewWithPosition = [(name: String, position: CGFloat)]()。如果我只保存一个单元格的位置可能是一样的,但由于我的单元格有不同的文本,我使用 name 参数来检查元素是否已经存在于数组中。我使用了辅助函数:

fileprivate func isElementInArray(array: [ViewAndPosition], string: String) -> Bool {
var bool = false
for views in array {
if views.name == string {
bool = true
return bool
}
}
return bool
}
  1. 当我有初始位置时,我检查可见标题是否错位:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if let visibleHeadersInSection = collectionView.visibleSupplementaryViews(ofKind: UICollectionElementKindSectionHeader) as? [CategoryNameReusableView] {
    for header in visibleHeadersInSection{
    if let index = viewWithPosition.index(where: { (element) -> Bool in
    element.name == header.nameLbl.text
    }){
    if header.frame.origin.y == viewWithPosition[index].position {
    header.backgroundColor = UIColor.clear
    } else {
    header.backgroundColor = UIColor.white
    }
    }
    }
    }

目前Apple似乎没有针对这种情况提供回调。希望这也是适合您的解决方案。

关于ios - 回调到 sectionHeadersPinToVisibleBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43987294/

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