標籤:
思路:若header的高度為25,在滑動的時候將scrollView的內容位移量上衣25,其實他還是粘在上面只不過我們看不到他了。
///---用於判斷往上滑還是往下滑
var deltaY:CGFloat = -111
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
deltaY = -111
}
//---------
override func scrollViewDidScroll(scrollView: UIScrollView) {
super.scrollViewDidScroll(scrollView)
if deltaY >= 0 {
if deltaY - scrollView.contentOffset.y > 0 {
let sectionHeaderHeight:CGFloat = 25;
if scrollView.contentOffset.y <= sectionHeaderHeight*2 && scrollView.contentOffset.y >= 0 {
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}else{
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}else{
let sectionHeaderHeight:CGFloat = 25;
if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0){
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}else{
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0)
}
}
}
else{
deltaY = scrollView.contentOffset.y
}
}
分析:
原理即在往上滑動的時候,將section header 的位移量往上移動header的高度 ,往下滑動則不管
在scrollViewDidScroll代理方法中
deltaY記錄可滑動開始是的位移量
通過滑動的距離的正負判斷上滑還是下滑,這個方法還能判斷scrollView上滑下滑。。。。
然後下滑時才設定位移量為25
scrollViewWillEndDragging方法中記錄當滑動停止時是初始位置失效
UITableView去掉section的header的粘性