iOS tableview 如何检查它是向上还是向下滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43268714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:03:36  来源:igfitidea点击:

iOS tableview how can I check if it is scrolling up or down

iosswiftuitableviewswift3

提问by Rome Torres

I am learning how to work with TableViews and I am wondering how can I figure out if the tableView is scrolling up or down ? I been trying various things such as this but it hasn't worked granted that below is for a scrollview and I have a TableView . Any suggestions would be great as I am new at this ...

我正在学习如何使用 TableViews,我想知道如何确定 tableView 是向上还是向下滚动?我一直在尝试诸如此类的各种事情,但它没有奏效,因为下面是滚动视图并且我有一个 TableView 。任何建议都会很棒,因为我是新手...

  func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
        print("down")
    } else {
        print("up")
    }
}

This is what I have in my tableView code

这就是我的 tableView 代码中的内容

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
        return Locations.count
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == self.Posts.count - 4 {

            reloadTable(latmin: self.latmin,latmax: self.latmax,lonmin: self.lonmin,lonmax: self.lonmax,my_id: myID)
            print("Load More")
        }

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! NewCell


   cell.post.text = Posts[indexPath.row]
   cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)

        return cell
    }

回答by Zonily Jame

Like @maddy said in the comment of your question, you can check if your UITableViewis scrolling by using the UIScrollViewDelegateand further more you could check which direction it scrolls to by using both scrollViewDidScrolland scrollViewWillBeginDraggingfunctions

就像@maddy 在您的问题的评论中所说的那样,您可以使用和函数来检查您UITableView是否正在滚动UIScrollViewDelegate,此外,您还可以使用scrollViewDidScrollscrollViewWillBeginDragging函数来检查它滚动到的方向

// we set a variable to hold the contentOffSet before scroll view scrolls
var lastContentOffset: CGFloat = 0

// this delegate is called when the scrollView (i.e your UITableView) will start scrolling
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    self.lastContentOffset = scrollView.contentOffset.y
}

// while scrolling this delegate is being called so you may now check which direction your scrollView is being scrolled to
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if self.lastContentOffset < scrollView.contentOffset.y {
        // did move up
    } else if self.lastContentOffset > scrollView.contentOffset.y {
        // did move down
    } else {
        // didn't move
    }
}

Furthermore: You don't need to subclass your UIViewControllerwith UIScrollViewDelegateif you've already subclassed your UIViewControllerwith UITableViewDelegatebecause UITableViewDelegateis already a subclass of UIScrollViewDelegate

此外:如果您已经对您的UIViewControllerwith进行了子类化,UIScrollViewDelegate则不需要将您的with子类化UIViewControllerUITableViewDelegate因为UITableViewDelegate它已经是UIScrollViewDelegate

回答by Alexandre Lara

You can implement scrollViewWillEndDraggingmethod:

您可以实现scrollViewWillEndDragging方法:

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


    if targetContentOffset.pointee.y < scrollView.contentOffset.y {
        // it's going up
    } else {
        // it's going down
    }

}

回答by sadegh bitarafan

first we should make two property for last Offset and Current Offset

首先我们应该为 last Offset 和 Current Offset 设置两个属性

 var lastOffset = CGFloat()
 var currentOffset = CGFloat()

then in the Scrollview VC call the scrollViewWillBeginDragging for get current offset

然后在 Scrollview VC 中调用 scrollViewWillBeginDragging 以获取当前偏移量

 override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

        self.lastOffset = scrollView.contentOffset.y

    }

and scrollViewWillBeginDecelerating for get last offset

和 scrollViewWillBeginDecelerating 获取最后一个偏移量

    override func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {

        self.lastOffset = scrollView.contentOffset.y
        viewStatus(scrollView: scrollView)

    }

then with this two variable you can know your scrollview W here it goes????

然后用这两个变量你可以知道你的滚动视图 W 在这里????

???let isScrollUp = scrollView.contentOffset.y > self.lastOffset
let isScrollDown  = scrollView.contentOffset.y < self.lastOffset