Radislav
Radislav

Reputation: 2993

How to determine that user finished vertical scrolling of tableview in objective c

I want to do some things when user finished vertical scrolling of table view. Does any body know how to determine that period?

Upvotes: 0

Views: 562

Answers (2)

MadNik
MadNik

Reputation: 7793

@implementation YourViewController
{
    BOOL shouldDoYourTask;
}

-(void)performYourTask{
    //Do Your Stuff
    shouldDoYourTask = YES;
}

#pragma mark - ScrollView Delegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView 
                  willDecelerate:(BOOL)decelerate
{
     if (!decelerate && shouldDoYourTask)
    {
        [self performYourTask];
        shouldDoYourTask = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (shouldDoYourTask) {
        [self performYourTask];
        shouldDoYourTask = NO;
    }

}

Upvotes: 1

Aaron Hayman
Aaron Hayman

Reputation: 8512

You need your view controller to become a delegate of UIScrollView: UIScrollViewDelegate

In your delegate you can implement the following methods to help determine the end state:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{

}
- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

}
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

}

There are also two properties of UIScrollView that can help:

scrollView.isDragging
scrollView.isDecelerating

Just note that there are several end 'possibilities' for the scroll view. If there is no deceleration, scrollViewDidEndDecelerating won't be called, only scrollViewDidEndDragging. However, if there is deceleration, both will be called. You can use the decelerate var in scrollViewDidEndDragging to help determine when to execute your code. For this reason, it's usually a good idea to have a separate method that's called by these delegate methods.

Upvotes: 4

Related Questions