UIRefreshControl endRefreshing is not smooth

Halpo picture Halpo · Feb 17, 2015 · Viewed 11.5k times · Source

When my data has finished loading and my tableView has been reloaded, I call endRefreshing on my refresh control, it then 'jumps' from its loading state and disappears - how can I implement a smooth animation that slides the refresh control away when it is complete?

Answer

Julian F. Weinert picture Julian F. Weinert · Dec 17, 2015

I'm adding a new answer since the only one is not very precise in explanation. And this would probably be too much for comments.

The solution by Halpo is correct. But the reason mentioned is wrong.

Calling -[NSObject (NSDelayedPerforming) performSelector:withObject:afterDelay:] guarantees the call to be performed in the next runloop iteration.

So this does not work, because there is a small delay, but you're moving it to the next loop.

Actually you sh/could decrease the delay to 0.0 and it would still work. Also, a more correct (in terms of using the object meant to achieve something, not better in outcome) implementation is possible.

Here are the various code snippets for that:

// SOLUTION I

[[self tableView] reloadData];
[[self refreshControl] performSelector:@selector(endRefreshing) withObject:nil afterDelay:0.0];

// SOLUTION II (semantically correct)

[[self tableView] reloadData];
[[NSOperationQueue currentQueue] addOperationWithBlock:^{
    [[self refreshControl] endRefreshing];
}];

// SOLUTION III (GCD)

dispatch_async(dispatch_get_main_queue(), ^{
    [[self refreshControl] endRefreshing];
}];

EDIT

As johann-fradj pointed out, you could also use GCD. I do like GCD a lot, but I think, solution II is most descriptive about whats going on. Also, GCD is not very Objective-C-ish, so I'd personally rather stick to my solution.