簡體   English   中英

如何使 UIScrollView 向上滾動效果為 UITableView

[英]How to make a UIScrollView scroll up effect as UITableView

我想讓我的滾動視圖像 UITableView 一樣上下滾動,即使內容大小適合設備屏幕大小。 但是向上滾動后,最終的內容大小必須與零的起始內容大小相同。 有沒有辦法做到這一點?

這里有一段代碼在用戶向下滾動時捕捉它的彈跳效果,如 UITableView 拉伸效果

    class ScrollViewController: UIScrollViewDelegate {

         private var topViewHeightConstraintConstant: CGFloat = 259
            @IBOutlet weak var TopViewHeightConstraint: NSLayoutConstraint!


                func scrollViewDidScroll(_ scrollView: UIScrollView) {
                    if scrollView === self.scrollView {
                        if scrollView.isBouncingBottom || scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height {
                            scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentSize.height - scrollView.frame.size.height), animated: false)
                        }
                        else if  scrollView.isBouncingTop && self.TopViewHeightConstraint.constant ==  topViewHeightConstraintConstant {
                            scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: 0), animated: false)
                        }
                        if scrollView.contentOffset.y < 0 {
                            if self.TopViewHeightConstraint.constant + abs(scrollView.contentOffset.y) <= topViewHeightConstraintConstant {
                                self.TopViewHeightConstraint.constant += abs(scrollView.contentOffset.y)
                            }
                        } else if scrollView.contentOffset.y > 0 &&  self.TopViewHeightConstraint.constant >=
                            (self.view.frame.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - self.contentViewHeight.constant) {
                            self.TopViewHeightConstraint.constant -= scrollView.contentOffset.y
                            scrollView.setContentOffset(CGPoint(x: scrollView.contentOffset.x, y: 0), animated: false)
                        }
                        view.layoutIfNeeded()
                    }
                }
            }

            func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
             if scrollView !== self.scrollView {
                  TopViewHeightConstraint.constant = topViewHeightConstraintConstant
                 }
             }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM