Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone projects needs "Pull-up to refresh" feature

Many iPhone projects use "Pull-to-Refresh" pattern to load more results (usually new data from the server.

In my project I want to do just the opposite: "pull-up to refresh". I want to load old data from the server but I need that the user request the data pulling up the UITableView.

How can I do it? Can anybody help me?

like image 578
Fernando Jordan Silva Avatar asked Oct 07 '11 07:10

Fernando Jordan Silva


1 Answers

Here's what I've been using:

First of all you have a view holding the "Pull up to refresh message", and assign it to:

[pullUpView setFrame:CGRectMake(0, [tableView rectForFooterInSection:0].origin.y, [tableView bounds].size.width,pullUpView.frame.height)];

Then you setup two delegate methods as below to track the dragging.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {    
    if (scrollView.isDragging) {
        CGFloat thresholdToRelease = [pullUpView frame].origin.y - [scrollView bounds].size.height;
        CGFloat thresholdToLoad = thresholdToRelease + [pullUpView frame].size.height;

        if (([scrollView contentOffset].y >= thresholdToRelease) && ([scrollView contentOffset].y < thresholdToLoad)) {
            [pullUpView reset];
        } else if ([scrollView contentOffset].y >= thresholdToLoad) {
            [pullUpView indicateThresholdRearched];
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    CGFloat thresholdToAction = [pullUpView frame].origin.y + [pullUpView frame].size.height - [scrollView bounds].size.height;

    if ([scrollView contentOffset].y >= thresholdToAction) {
        if (!performingAction) {

            [pullUpView startLoading];

            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.5];
            [tableView setContentInset:UIEdgeInsetsMake(0, 0, [pullUpView frame].size.height, 0)];
            [UIView commitAnimations];

            /* do your things here */
            performingAction = YES;
        }
    }
}

At the end revert the tableView's contentInset to hide the pullUpView.

like image 121
Nevin Avatar answered Oct 15 '22 08:10

Nevin