jordelver
jordelver

Reputation: 8432

UITableView scroll to a specific point when scrollViewDidEndDragging

I have a UITableView which is scrollable. When the user scrolls the UITableView a certain amount and then releases their finger, instead of scrolling back to the top, I want it to scroll 50 points from the top, effectively leaving a gap of 50 points between the top of the screen and the top most cell.

At the moment I have implemented scrollViewDidEndDragging, but I don't know how to set where the view will scroll back to.

I've tried the following, but it doesn't work as desired.

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // If scrolled over 100 points
    if(-100 > (double)self.tableView.contentOffset.y) {
        [self.tableView setContentOffset:CGPointMake(0, -50) animated:YES];
    }
}

The code above causes a weird flicker, like the animations are both running concurrently, but I'm unsure.

Any help would be appreciated.

UPDATE with more information

I'm implementing "Pull down to refresh" functionality based upon this post: https://stackoverflow.com/a/6392145/120615

I've added an extra UIView to the top of the UITableView and when you scroll the UITableView, the UIView moves down into view. When you release your finger from the screen, the UITableView scrolls back to the top hiding the new UIView I've added.

What I would like to happen is that when you release your finger the UITableView scrolls back to top but stops 100 points from the top (so I can show an activity indicator to the user) and then after 2/3 seconds scrolls completely to the top.

Upvotes: 1

Views: 2066

Answers (1)

RBI
RBI

Reputation: 833

I think you are right, the animations are running concurrently because scrollviews have the deceleration feature.

instead of using the DidEndDraging callback function try this one:

  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

This will only be called once the user triggered animation is complete.

Upvotes: 2

Related Questions