UITableView去掉section的header的粘性

來源:互聯網
上載者:User

標籤:

思路:若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的粘性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.