簡書詳情滾動效果

研究了老半天,早上醒來吹了一會電風扇突然頓悟,不過不知道是不是和簡書的思路一樣的。

代碼其實非常簡單

只需要一個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;
}
}
}
}

demo

https://github.com/ryanypm/demoScrollView

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容