首页 > 解决方案 > 如何知道 CollectionView 中 Header 的特定部分是否滚动到屏幕边界之外?

问题描述

我想在我的段控件超出屏幕范围时立即获得回调或通知。我的收藏视图顶部有一个自定义标题视图。

过去,当我的 Header 视图中只有 1 个 UIElement 时,我使用了这些方法

func collectionView(UICollectionView, willDisplaySupplementaryView: UICollectionReusableView, forElementKind: String, at: IndexPath)

func collectionView(UICollectionView, didEndDisplayingSupplementaryView: UICollectionReusableView, forElementOfKind: String, at: IndexPath)

实现这个回调,这样我就可以在发生这种情况时调用我的特定函数。

但我现在有一个更复杂的标题,只有几个 UIElements。有没有一种方法可以在 Segment 控件超出屏幕截图的屏幕范围时立即获得通知或回调?在此处输入图像描述

标签: iosswiftxcodeswift3uicollectionview

解决方案


您是否尝试过如下实施scrollViewDidScroll方法?

var isSegmentedHidden = false
func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if scrollView.contentOffset.y < 50.0 && isSegmentedHidden{ //Here 50.0 is the height of your segmented control plus vertical padding if any.

        isSegmentedHidden = false

        //Call your function here, once segmented control is visible
    }

    if scrollView.contentOffset.y >= 50.0 && isSegmentedHidden == false{ //Here 50.0 is the height of your segmented control plus vertical padding if any.

        isSegmentedHidden = true

        //Call your function here, once segmented control is invisible
    }

}

推荐阅读