【问题标题】:Disable UIScrollView bounce at the top of the screen禁用屏幕顶部的 UIScrollView 弹跳
【发布时间】:2014-03-29 20:07:17
【问题描述】:

我的应用中有 UIScrollView 并启用了垂直反弹。我需要在屏幕底部反弹,但在屏幕顶部没有启用。

我不知道如何使用以下方法检测滚动视图:

  (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

  // no bounce if scrollView has reached the top of the screen ??
  }

【问题讨论】:

    标签: ios iphone ipad


    【解决方案1】:

    尝试在滚动视图滚动时根据内容偏移量设置bounces 属性:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        scrollView.bounces = (scrollView.contentOffset.y > 100);
    }
    

    【讨论】:

    • 我想了解100 的值从何而来?为什么正好是100?您对此@Wain 有解释吗?
    • @BrunoBieri 这是用户测试中任意选择的值,以获得正确的感觉并防止误报。视图的大小意味着可达到的精度水平不需要小数位。许多值可能会根据滚动视图和内容的大小起作用...
    • 好的,我明白了。谢谢你。从原始答案中我不清楚。这对我来说意味着包含视图需要至少有 100 的高度才能使此代码正常工作。
    【解决方案2】:

    SWIFT

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //disable bounce only at the top of the screen
        scrollView.bounces = scrollView.contentOffset.y > 100
    }
    

    【讨论】:

      【解决方案3】:

      在滚动发生之前满足条件是正确的

      override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
         tableView.bounces = tableView.contentOffset.y > 100
      }
      

      【讨论】:

        【解决方案4】:

        仅用于拉动:

        extension YourViewController: UIScrollViewDelegate {
            
            func scrollViewDidScroll(_ scrollView: UIScrollView) {
                scrollView.contentOffset.y = min(-88, scrollView.contentOffset.y) // -88 or other point
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-05-14
          • 2017-11-19
          • 1970-01-01
          • 1970-01-01
          • 2018-03-29
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多