网上关于给UITableView实现下拉刷新效果的帖子较多,但是给UIWebView实现的比较少。还好在code4app网站上找到一篇给UIScrollView和UITableView实现下拉刷新的demo【下载地址】。它其中用到的开源控件是EGORefreshTableHeaderView。虽然名字是EGORefreshTableHeaderView,但是它其实可以用于所有UIScrollView,而UIWebView其实也算是一个UIScrollView,所以该控件也是可以用于UIWebView的。
我参照code4app的例子给我的UIWebView加上了下拉刷新的功能,在UIWebView上使用稍微有点不一样的地方。
.h文件中主要代码如下
#import <UIKit/UIKit.h>
#import "EGORefreshTableHeaderView.h"
#define URL_INDEX "http://www.sogou.com"
@interface ViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate, EGORefreshTableHeaderDelegate> {
//下拉视图
EGORefreshTableHeaderView * _refreshHeaderView;
//刷新标识,是否正在刷新过程中
BOOL _reloading;
}
@property(weak, nonatomic) IBOutlet UIWebView *uiWebView;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize uiWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
//webview 和 webview 的 scrollView 的委托都设置为self
self.uiWebView.delegate = self;
self.uiWebView.scrollView.delegate = self;
[self loadPage];
//初始化refreshView,添加到webview 的 scrollView子视图中
if (_refreshHeaderView == nil) {
_refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0-self.uiWebView.scrollView.bounds.size.height, self.uiWebView.scrollView.frame.size.width, self.uiWebView.scrollView.bounds.size.height)];
_refreshHeaderView.delegate = self;
[self.uiWebView.scrollView addSubview:_refreshHeaderView];
}
[_refreshHeaderView refreshLastUpdatedDate];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
//加载网页
- (void)loadPage {
NSURL *url = [[NSURL alloc] initWithString:@URL_INDEX];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.uiWebView loadRequest:request];
}
#pragma mark - webview delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
_reloading = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
_reloading = NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.uiWebView.scrollView];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"load page error:%@", [error description]);
_reloading = NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.uiWebView.scrollView];
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
[self loadPage];
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
return _reloading; // should return if data source model is reloading
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
return [NSDate date]; // should return date data source was last changed
}
@end
这里需要注意的是,几处关于用到UIWebView尺寸的地方不是self.uiWebView.bounds,而应该是self.uiWebView.scrollView.bounds。这也说明UIWebView与UIScrollView不是继承的关系,而是组合的关系。
此Demo的下载地址:
http://code4app.com/ios/UIWebView-%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0/51aee3c16803fa770f000003
参考: