UIRefreshControl - How do I make the refresh action occur after the touch is released?

outphase picture outphase · Mar 23, 2013 · Viewed 25.1k times · Source

Long time listener, first time caller here on stack overflow. Be gentle.

I am implementing UIRefreshControl on a UITableView to refresh the table's data. On other pull-to-refresh implementations, the refresh process does not begin until the user's finger is lifted while in the pull's refresh distance. UIRefreshControl does not immediately seem like it has this customization.

My UIRefreshControl init code:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];

My refresh: code is fairly basic:

-(void)refresh:(id)sender {
(... refresh code ...)
[sender endRefreshing];
}

How can I delay the refresh: function until the user removes their finger from the pull?

Answer

Ned picture Ned · Mar 23, 2013

I've also stuck with the same problem. I don't think that my approach is very nice, but seems like it works.

  1. Init UIRefreshControl

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    self.refreshControl = refreshControl;
    
  2. Check state of UIRefreshControl when user finish dragging the table (UITableViewDelegate conforms to UIScrollViewDelegate)

    - (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
    {    
        if( self.refreshControl.isRefreshing )
            [self refresh];
    }
    
  3. Update table

    - (void)refresh
    {
        [self.refreshControl endRefreshing];
    
        // TODO: Update here your items
    
        [self.tableView reloadData];
    }
    

Hope it will help you.