Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIRefreshControl endRefreshing is not smooth

Tags:

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?

like image 827
Halpo Avatar asked Feb 17 '15 10:02

Halpo


2 Answers

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.

like image 158
Julian F. Weinert Avatar answered Oct 03 '22 02:10

Julian F. Weinert


Fixed the problem - I added a tiny delay to endRefresh after reloading data:

[self.tableView reloadData]; [self.refreshControl performSelector:@selector(endRefreshing) withObject:nil afterDelay:0.05]; 
like image 24
Halpo Avatar answered Oct 03 '22 03:10

Halpo