How can I detect the scroll direction from the UICollectionView?

后端 未结 4 845
日久生厌
日久生厌 2020-12-09 17:52

I have a UICollectionView. I want to detect scroll direction. I have a two different animation style for scroll down and scroll up. So I must learn scroll direction.

4条回答
  •  被撕碎了的回忆
    2020-12-09 18:42

    Swift 4.2

    private var lastContentOffset: CGFloat = 0
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        if lastContentOffset > scrollView.contentOffset.y && lastContentOffset < scrollView.contentSize.height - scrollView.frame.height {
            // move up
            print("move up")
            originalHeight ()
        } else if lastContentOffset < scrollView.contentOffset.y && scrollView.contentOffset.y > 0 {
            // move down
            print("move down")
            minimizeHeaderView()
        }
    
        // update the new position acquired
        lastContentOffset = scrollView.contentOffset.y
    }
    

提交回复
热议问题