【问题标题】:How to make a UIScrollView scroll up effect as UITableView如何使 UIScrollView 向上滚动效果为 UITableView
【发布时间】:2020-03-15 06:41:26
【问题描述】:

我想让我的滚动视图像 UITableView 一样上下滚动,即使内容大小适合设备屏幕大小。但是向上滚动后,最终内容大小必须与起始内容大小相同,即为零。有什么办法吗?

【问题讨论】:

  • 表格视图建立在滚动视图之上,如果内容尺寸小或适合屏幕,滚动视图将无法滚动。您的意思是根据您的滚动/滑动手势调整内容的大小吗?
  • 是的,我想让用户可以向上滚动滚动视图,但是当她/他完成滚动事件时,滚动视图再次自动将其内容大小设置为起始值(零)像表格视图一样流畅的动画
  • 你想要弹跳效果吗?
  • 是的上下弹跳效果

标签: ios uitableview uiscrollview


【解决方案1】:

这里有一段代码在用户向下滚动时捕捉它的弹跳效果,就像 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
                 }
             }
}

【讨论】:

  • 谢谢,我会试试的。故事板界面构建器中有什么简单的方法吗?
  • 您可以从情节提要中为 TopViewHeightConstraint 创建一个 IBOutlet
猜你喜欢
  • 1970-01-01
  • 2014-01-02
  • 2017-08-24
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多