向上滾動時(shí)锌半,先 rootScrollView 向上滾動,等到 header 消失加酵,containerScrollView 到達(dá)頂部時(shí)拳喻,containerScrollView 再開始滾動。向下滾動時(shí)猪腕,等到 containerScrollView 滾動到頂部不能繼續(xù)滾動時(shí)冗澈,rootScrollView 才可以開始滾動,顯示 header陋葡。
方法
根據(jù) scrollView 的都偏移量亚亲,設(shè)置 rootScrollView 和 containerScrollView 的 isScrollEnabled。缺點(diǎn):scrollView 的 isScrollEnabled 切換時(shí)腐缤,平移手勢會中斷捌归,滾動不連貫。
rootScrollView 和 containerScrollView 可以同時(shí)識別平移手勢岭粤,限制其 contentOffset 惜索。(下面會詳細(xì)描述)
設(shè)置 rootScrollView 和 containerScrollView 可以同時(shí)滾動
containerScrollView.bounces = false,否則也會出現(xiàn)滑動不連貫
rootScrollView 可以滾動時(shí)剃浇,containerScrollView 不能滾動巾兆,保持 contentOffset 不變
containerScrollView 可以滾動時(shí),rootScrollView 不能滾動虎囚,保持 contentOffset 不變
代碼實(shí)例
設(shè)置 rootScrollView 和 containerScrollView 可以同時(shí)滾動
只要其中一個(gè) scrollView 實(shí)現(xiàn)了 UIGestureRecognizerDelegate角塑,并且 gestureRecognizer(_:shouldRecognizeSimultaneouslyWith otherGestureRecognizer:) 允許和另一個(gè) scrollView 同時(shí)識別平移手勢。這里簡單起見淘讥,直接返回 true圃伶。
class MyScrollView: UIScrollView, UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
rootScrollView 可以滾動時(shí),containerScrollView 不能滾動,保持 contentOffset 不變
保持 containerScrollView 的 contentOffset.y 不變窒朋,在 containerScrollView 的 delegate 的 scrollViewDidScroll(:) 中設(shè)置 contentOffset.y = 0搀罢。在設(shè)置之前,要判斷 containerScrollView 能否滾動炼邀。不能根據(jù) containerScrollView.contentOffset.y 判斷魄揉,因?yàn)檫M(jìn)入 scrollViewDidScroll(:) 時(shí),containerScrollView.contentOffset.y 已經(jīng)發(fā)生改變拭宁。所以判斷條件為洛退,rootScrollView.contentOffset.y 等于 containerScrollView 在 rootScrollView 中的偏移量時(shí),containerScrollView 可以滾動杰标。
var containerScrollViewOffsetY: CGFloat {
return containerScrollView.convert(containerScrollView.bounds, to: rootScrollView).minY
}
func canContainerScrollViewScroll() -> Bool {
return rootScrollView.contentOffset.y >= containerScrollViewOffsetY
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView === containerScrollView {
guard !canContainerScrollViewScroll() else {
return
}
containerScrollView.contentOffset.y = 0
}
}
containerScrollView 可以滾動時(shí)兵怯,rootScrollView 不能滾動,保持 contentOffset 不變
同理腔剂,在 rootScrollView 的 scrollViewDidScroll(_:) 中媒区,不能根據(jù) rootScrollView.contentOffset.y 判斷 rootScrollView 是否可以滾動,而是通過
containerScrollView.contentOffset.y == 0 判斷掸犬。
func canRootScrollViewScroll() -> Bool {
return containerScrollView.contentOffset.y == 0
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView === rootScrollView {
guard !canRootScrollViewScroll() else {
return
}
scrollView.contentOffset.y = containerScrollViewOffsetY
}
}
完整代碼
import UIKit
import SnapKit
class ViewController: UIViewController {
let rootScrollView = MyScrollView()
let headerView = UIView()
let containerScrollView = UIScrollView()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupUI()
}
func setupUI() {
let rootView = UIView()
view.addSubview(rootView)
rootView.snp.makeConstraints { make in
make.edges.equalTo(view.safeAreaLayoutGuide)
}
rootView.addSubview(rootScrollView)
rootScrollView.addSubview(headerView)
rootScrollView.addSubview(containerScrollView)
containerScrollView.addSubview(label)
rootScrollView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
rootScrollView.delegate = self
// rootScrollView.bounces = false
headerView.backgroundColor = .blue
headerView.snp.makeConstraints { make in
make.left.right.top.equalToSuperview()
make.width.equalTo(rootView)
make.height.equalTo(100)
}
containerScrollView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
make.top.equalTo(headerView.snp.bottom)
make.height.equalTo(rootView)
}
containerScrollView.delegate = self
// 2. containerScrollView.bounces = false
containerScrollView.bounces = false
label.snp.makeConstraints { make in
make.width.equalTo(rootView)
make.edges.equalToSuperview()
}
label.numberOfLines = 0
var text = ""
for i in 1...100 {
text += "\(i)\n"
}
label.text = text
label.backgroundColor = .lightGray
}
var containerScrollViewOffsetY: CGFloat {
return containerScrollView.convert(containerScrollView.bounds, to: rootScrollView).minY
}
func canRootScrollViewScroll() -> Bool {
return containerScrollView.contentOffset.y == 0
}
func canContainerScrollViewScroll() -> Bool {
return rootScrollView.contentOffset.y >= containerScrollViewOffsetY
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView === rootScrollView {
guard !canRootScrollViewScroll() else {
return
}
// 4. scrollView 不能滾動袜漩,保持 contentOffset 不變
scrollView.contentOffset.y = containerScrollViewOffsetY
} else if scrollView === containerScrollView {
guard !canContainerScrollViewScroll() else {
return
}
// 3. containerScrollView 不能滾動,保持 contentOffset 不變
containerScrollView.contentOffset.y = 0
}
}
}
class MyScrollView: UIScrollView, UIGestureRecognizerDelegate {
// 1. 設(shè)置 rootScrollView 和 containerScrollView 可以同時(shí)滾動
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
作者:藍(lán)色的海洋_11fb
鏈接:http://www.reibang.com/p/b2bcffafafc5
來源:簡書
著作權(quán)歸作者所有湾碎。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)宙攻,非商業(yè)轉(zhuǎn)載請注明出處。
想要知道tableView是否滑動到了底部,我們需要先了解一些有關(guān)于scrollView的概念(UITableView繼承自UIScrollView)(查了資料后的個(gè)人理解.如有不對請幫忙指出)
contentSize: 可以滾動的區(qū)域
contentOffset: scrollView頂點(diǎn)相對于contentView頂點(diǎn)的偏移量
contentInset: contentView四周距離scrollView的距離.
知道了這幾個(gè)詞的概念后,我們回到tableView上來.
首先,使用- (void)scrollViewDidScroll:(UIScrollView*)scrollView監(jiān)聽tableView的滾動,并打印scrollView.contentOffset的y值和scrollView.contentSize的height值,在往底部滾動的過程中,會發(fā)現(xiàn),scrollView.contentOffset.y越來越大,越來越接近scrollView.contentSize.height的值.直到滾到底部,兩者相差一個(gè)屏幕的高度(我使用6測試的)
通過畫圖分析,也不難得出結(jié)果
為使容判斷到達(dá)底部的容錯(cuò)能力稍強(qiáng),兩者相減<=ScreenWidth + 1,就可以得到比較準(zhǔn)確的效果了.而且因?yàn)橛袝r(shí),這兩個(gè)值相減不一定是整數(shù),+1也是為了避免因?yàn)檫@種情況出現(xiàn)導(dǎo)致判斷錯(cuò)誤.
判斷代碼:
if(delta <= 1 + [UIScreen mainScreen].bounds.size.height)
{
NSLog(@"YES");
}
else
{
NSLog(@"NO");
}
以上只分析了contentInset四個(gè)值都為0的情況.設(shè)置了contentInset也同理可以通過打印值,修改判斷的條件得到答案.
</article>
11人點(diǎn)贊
作者:Accepted_
鏈接:http://www.reibang.com/p/7b095d12235c
來源:簡書
著作權(quán)歸作者所有介褥。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)座掘,非商業(yè)轉(zhuǎn)載請注明出處。