【问题标题】:Stop overscroll UITableView only at the top?仅在顶部停止过度滚动 UITableView?
【发布时间】:2016-06-25 15:54:26
【问题描述】:

有类似的问题Stop UITableView over scroll at top & bottom?,但我需要稍微不同的功能。我想要我的表格,以便它可以在底部过度滚动,但不能在顶部过度滚动。 据我了解,

tableView.bounces = false

允许在顶部和底部禁用过度滚动,但是,我只需要在顶部禁用它。喜欢

tableView.bouncesAtTheTop = false
tableView.bouncesAtTheBottom = true

【问题讨论】:

    标签: ios objective-c swift uitableview


    【解决方案1】:

    对于 Swift 2.2,使用

    func scrollViewDidScroll(scrollView: UIScrollView) {
        if scrollView == self.tableView {
            if scrollView.contentOffset.y <= 0 {
                scrollView.contentOffset = CGPoint.zero
            }
        }
    }
    

    对于目标 C

        -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        if (scrollView.contentOffset.y<=0) {
            scrollView.contentOffset = CGPointZero;
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可以通过改变tableView的scrollViewDidScroll中的bounce属性来实现(你需要成为tableView的delegate)

      拥有lastY的属性:

      var lastY: CGFloat = 0.0
      

      viewDidLoad中将初始反弹设置为false:

      tableView.bounces = false
      

      和:

      func scrollViewDidScroll(scrollView: UIScrollView) {
          let currentY = scrollView.contentOffset.y
          let currentBottomY = scrollView.frame.size.height + currentY
          if currentY > lastY {
              //"scrolling down"
              tableView.bounces = true
          } else {
              //"scrolling up"
              // Check that we are not in bottom bounce
              if currentBottomY < scrollView.contentSize.height + scrollView.contentInset.bottom {
                  tableView.bounces = false
              }
          }
          lastY = scrollView.contentOffset.y
      }
      

      【讨论】:

      • 你可以检查scrollView.contentOffset.y是否小于0.0。如果是,只需将其设置为 0.0。这样,scrollView 将无法仅在“向上”方向过度滚动
      猜你喜欢
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      • 2011-02-22
      • 2013-01-20
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多