Swift - UITableView scroll event

萝らか妹 提交于 2020-01-12 12:54:28

问题


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.view.endEditing(true).

Thanks in advance


回答1:


You can add UIScrollViewDelegate. After that you can implement scrollViewDidScroll method.




回答2:


You can set property of UITable view (XCode 7+)

In Storyboard:

in Code:

tableView.keyboardDismissMode = .onDrag



回答3:


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.




回答4:


override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

        if(velocity.y>0){
            NSLog("dragging Up");
        }else{
            NSLog("dragging Down");
        }
    }


来源:https://stackoverflow.com/questions/30189530/swift-uitableview-scroll-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!