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 m开发者_Python百科y 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?
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.
"Pull up to refresh" sounds a bit odd, especially if the table is more than one screen long. But if you really want to do it, check out the Three20 open source library. There you'll find the "pull down to refresh" functionality, and you can just adapt it to suit your needs.
精彩评论