Swift 4 UICollectionView detect end of scrolling

前端 未结 5 1563
野趣味
野趣味 2021-02-04 13:13

I have an Horizontal UICollectionView on my app and I want to load more data when the user reaches the end (or nearly to the end) of UICollectionView w

5条回答
  •  爱一瞬间的悲伤
    2021-02-04 13:44

    Implement method scrollViewDidScroll of UIScrollViewDelegate:

    var isLoading: Bool = false
    
     func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let contentOffsetX = scrollView.contentOffset.x
        if contentOffsetX >= (scrollView.contentSize.width - scrollView.bounds.width) - 20 /* Needed offset */ {
            guard !self.isLoading else { return }
            self.isLoading = true
            // load more data
            // than set self.isLoading to false when new data is loaded
        }
    }
    

提交回复
热议问题