20

Is there a way to check if my tableview just finished scrolling? table.isDragging and table.isDecelerating are the only two methods that I can find. I am not sure how I can either anticipate or get notified when the tableview finishes scrolling.

Can I somehow use timers to check every second if the tableView is scrolling or not?

khelwood
  • 55,782
  • 14
  • 81
  • 108
sankaet
  • 345
  • 2
  • 5
  • 12

4 Answers4

37

You would implement UIScrollViewDelegate protocol method as follows:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (!decelerate) {
        [self scrollingFinish];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollingFinish];
}

- (void)scrollingFinish {
    //enter code here
}

Swift version

public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if decelerate {
        scrollingFinished()
    }
}

public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    scrollingFinished()
}

func scrollingFinished() {
    print("scrolling finished...")
}

For the above delegate method The scroll view sends this message when the user’s finger touches up after dragging content. The decelerating property of UIScrollView controls deceleration. When the view decelerated to stop, the parameter decelerate will be NO.

Second one used for scrolling slowly, even scrolling stop when your finger touch up, as Apple Documents said, when the scrolling movement comes to a halt.

shanegao
  • 3,562
  • 1
  • 18
  • 21
  • 2
    User can also invoke table scroll by tapping the top of the screen. Unfortunately, in this case scrollViewDidEndDecelerating isn't being called, so we have to implement scrollViewDidScrollToTop to handle this case. – Jovan Stankovic Jun 27 '17 at 09:28
  • This answer is best, and @JovanStankovic, you are very correct. – Nikhil Manapure Oct 05 '17 at 08:49
7

The below code will update you every time when user scrolling stopped.

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate)
    {
        if (isScrollingStart)
        {
            isScrollingStart=NO;
            [self scrollingStopped];
        }
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    if (isScrollingStart)
    {
        isScrollingStart=NO;
        [self scrollingStopped];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    isScrollingStart=YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    isScrollingStart=YES;
}
-(void)scrollingStopped
{
    NSLog(@"Scrolling stopped");
}
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
  • Works very nicely, thank you. The value `isScrollingEnd` is written but never used. Is there just in case you need to use it? Did you meant to check it in scrollingStopped? – oyalhi Jan 10 '16 at 09:28
  • Updated ans. , there is no use of isScrollingEnd here :) – Pandey_Laxman Jan 11 '16 at 06:30
1

UITableView conforms to UIScrollViewDelegate. Please, refer to the documentation of that protocol, it has methods you need.

Jeepston
  • 1,361
  • 7
  • 7
0

After using shanegao's answer and Jovan Stankovic's comment on it, I devised this for Swift3 -

extension NMViewController: UIScrollViewDelegate {
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            scrollViewDidEndDecelerating(scrollView)
        }
    }

    func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
        scrollViewDidEndDecelerating(scrollView)
    }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        // Your logic to handle after scrolling is done
    }
}
Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55