研究了老半天,早上醒來吹了一會電風扇突然頓悟,不過不知道是不是和簡書的思路一樣的。
代碼其實非常簡單
只需要一個tableView和webView
創(chuàng)建WebView
UIWebView *webView = [[UIWebView alloc] init];
webView.frame = self.view.bounds;
webView.delegate = self;
webView.scrollView.delegate = self;
創(chuàng)建tableView
UITableView *tableView = [[UITableView alloc] init];
tableView.delegate = self;
tableView.dataSource = self;
tableView.tableHeaderView = webView;
tableView.bounces = NO;
tableView.scrollEnabled = NO;
tableView.frame = CGRectMake(0, 0, viewWidth, viewHeight);
[self.view addSubview:tableView];
self.tableView = tableView;
由于webView在頂部一開始有彈簧收縮效果。tableView在底部,一開始是不需要的,所以暫時禁用tableView的bounces和scrollEnabled。不能一開始就可以讓tableView滾動,不然就GG了.
監(jiān)聽滾動條的變化
然后就是各種判斷是否要禁用scrollEnabled和設(shè)置bounces的NO/YES了
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat top = scrollView.contentOffset.y;
if ([scrollView isKindOfClass:[UITableView class]]) {
if (top > (_tableView.contentSize.height - viewHeight - 100)) {
_tableView.bounces = YES;
}else{
_tableView.bounces = NO;
}
}else if(scrollView == _webView.scrollView){
if (top > 30) {
_tableView.bounces = NO;
_webView.scrollView.bounces = NO;
if (top >= _webView.scrollView.contentSize.height - viewHeight - 100) {
_tableView.bounces = YES;
_tableView.scrollEnabled = YES;
}
}else{
_tableView.bounces = NO;
_webView.scrollView.bounces = NO;
_tableView.scrollEnabled = NO;
if (top < 30) {
_webView.scrollView.bounces = YES;
}
}
}
}