Skip to content

Instantly share code, notes, and snippets.

@jberkman
Last active September 3, 2015 16:51
Show Gist options
  • Save jberkman/8779769 to your computer and use it in GitHub Desktop.
Save jberkman/8779769 to your computer and use it in GitHub Desktop.
- (void)refresh:(id)sender
{
double delayInSeconds = 5.0;
UIRefreshControl * __weak refreshControl = self.refreshControl;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// Calling [self.refreshControl endRefreshing] while tableView is
// decelerating causes it to stop abruptly. performSelector:...afterDelay:
// defers this until after decelerating completes.
[refreshControl performSelector:@selector(endRefreshing)
withObject:nil
afterDelay:0];
});
}
@filblue
Copy link

filblue commented Mar 18, 2015

Hey, here`s my take on this, what do you think?

@property BOOL shouldEndRefreshingWhenDecelerationEnds;

- (void)endRefreshing
{
    if ([self.collectionView isDecelerating]) {
        self.shouldEndRefreshingWhenDecelerationEnds = YES;
    } else {
        [self.refreshControl endRefreshing];
    }
}

- (void)onRefresh
{
    [self.feedViewModel reload:@weakselfnotnil(^) {
        [self endRefreshing];
    } @weakselfend failure:@weakselfnotnil(^(NSError *error)) {
        [self endRefreshing];
    } @weakselfend];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.shouldEndRefreshingWhenDecelerationEnds) {
        self.shouldEndRefreshingWhenDecelerationEnds = NO;
        [self.refreshControl endRefreshing];
    }
}

@julian-weinert
Copy link

Great, @jberkman!

Since you asked, @filblue – I like the original idea better.
It's ways more simple and depends on only one line of code, not an additional property, another method that needs to be called and so on.

I have to admit – before I came across this gist, I did it the same way than you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment