Swift - UITableView scroll event

后端 未结 4 1013
深忆病人
深忆病人 2021-02-18 14:22

I was wondering how to detect if the UITableView is scrolled (up or down). I want to hide the keyboard when the UITableView is scrolled with self

4条回答
  •  既然无缘
    2021-02-18 14:56

    I believe the complete solution would be the following:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == feedTableView {
            let contentOffset = scrollView.contentOffset.y
            print("contentOffset: ", contentOffset)
            if (contentOffset > self.lastKnowContentOfsset) {
                print("scrolling Down")
                print("dragging Up")
            } else {
                print("scrolling Up")
                print("dragging Down")
            }
        }
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if scrollView == feedTableView {
            self.lastKnowContentOfsset = scrollView.contentOffset.y
            print("lastKnowContentOfsset: ", scrollView.contentOffset.y)
        }
    }
    

    The previous answers weren't 100% accurate.

    Explanation: scrollViewDidEndDragging will be called when the scrolling stops, therefore we save the last know offset. After that we compare it with the current offset in the delegate method scrollViewDidScroll.

提交回复
热议问题