项目中需求:向上滑动上层的ScrollView时,底层ScrollView优先滑动,当底层ScrollView滑动到最大值时,上层ScrollView继续滑动,向下滑动时,上层ScrollView滑动到最小值时,底层ScrollView开始滑动
代码保存
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let vc = self.childVcs[currentVCIndex]
if scrollView == vc.tableView {
let vcY = vc.tableView?.contentOffset.y ?? 0
let backY:Int = Int(backScrollView.contentOffset.y)
let newY = scrollView.contentOffset.y
print(newY)
if scrollView == vc.tableView {
if vcY >= 0 && backY != Int(headerHeight) { // 让底层ScrollView向上滑动
if ((backY - Int(headerHeight)) > 0) {
backScrollView.contentOffset = CGPoint.init(x: 0, y: Int(headerHeight))
// return
} else {
backScrollView.contentOffset = CGPoint.init(x: 0, y: CGFloat(backY) + vcY)
}
vc.tableView?.contentOffset = CGPoint.init(x: 0, y: 0)
} else if backY <= Int(headerHeight) && newY < 0 && backY > 0 { // 让底层ScrollView向下滑动
if backY < 0 {
backScrollView.contentOffset = CGPoint.init(x: 0, y: 0)
} else {
backScrollView.contentOffset = CGPoint.init(x: 0, y: CGFloat(backY) + vcY)
}
vc.tableView?.contentOffset = CGPoint.init(x: 0, y: 0)
} else {
// return
}
}
}
}