【问题标题】:How Do I Shorten the Pull Distance on UIRefreshControl to Activate the Pull to Refresh Action?如何缩短 UIRefreshControl 上的拉动距离以激活拉动刷新操作?
【发布时间】:2013-12-11 23:14:36
【问题描述】:

嘿 StackOverflow 的人,

我一直在尝试解决这个问题一段时间,但无济于事,我需要一些帮助。我的应用程序底部有一个UITableView,但屏幕距离不足,用户无法进行刷新。有人知道我如何缩短在UITableViewUIWebView 内对UIRefreshControl 激活拉动刷新操作所需的距离吗?

提前谢谢大家!

【问题讨论】:

标签: ios uitableview uiwebview uirefreshcontrol


【解决方案1】:

对于 Swift 3.2 及以上版本:

var canRefresh = true

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < -100 {
        if canRefresh && !self.refreshControl.isRefreshing {
            self.canRefresh = false
            self.refreshControl.beginRefreshing()
            self.handleRefresh()
        }
    } else if scrollView.contentOffset.y >= 0 {
        self.canRefresh = true
    }
}

【讨论】:

    【解决方案2】:

    你可以通过一些修改使其正确

    lazy var refreshControl: UIRefreshControl = {
            let refreshControl = UIRefreshControl()
            refreshControl.tintColor = UIColor.red
            return refreshControl
        }()
    
    //refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
    

    每个 PullToRefresh 都必须有几行这样的代码,handleRefresh 函数,做任何你需要刷新页面的事情。

    您只需注释掉 addTarget 行并将此函数添加到您的代码中 ```

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if scrollView.contentOffset.y < -80 { //change 80 to whatever you want
                if  !self.refreshControl.isRefreshing {
                    handleRefresh()
                }
            }
        }
    

    我在 Ashkan Ghodrat 的回答的帮助下编写了这段代码

    【讨论】:

      【解决方案3】:

      您仍然可以使用 refreshControl,但需要进行一些修改!

      将这些代码添加到您的 viewController:

      var canRefresh = true
      
      override func scrollViewDidScroll(scrollView: UIScrollView) {
      
          if scrollView.contentOffset.y < -100 { //change 100 to whatever you want
      
              if canRefresh && !self.refreshControl.refreshing {
      
                  self.canRefresh = false
                  self.refreshControl.beginRefreshing()
      
                  self.refresh() // your viewController refresh function
              }
          }else if scrollView.contentOffset.y >= 0 { 
      
              self.canRefresh = true
          }
      }
      

      和往常一样,在 self.refresh() 函数中添加刷新逻辑:

         self.refreshControl.endRefreshing()
      

      【讨论】:

      • 好东西!
      • 谢谢!这正是我希望找到的。
      • 感谢 canRefresh 破解。没有它,refreshcontrol 的大小会随机变化。
      • 这显然应该是选择的答案!!
      【解决方案4】:

      根据 Apple Docs,我没有看到任何修改 UIRefreshControl 参数的方法。
      链接:https://developer.apple.com/library/ios/documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html

      使用像ODRefreshControl这样的第三方组件(自定义滚动距离以激活刷新,修改 #define kMaxDistance 常量)。

      或者...

      不要使用UIRefreshControl,而是在-scrollViewDidScroll 方法中实现自己的逻辑,例如:

      - (void)scrollViewDidScroll:(UIScrollView *)scrollView
      {
          if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height) {
              //refresh logic
          }
      }
      

      【讨论】:

      • ODRefreshControl 是否有影响拉距的可修改参数?我正在看它,但没有看到任何东西。或者我今天可能只是瞎了眼,有严重的隧道视力。
      • @DanielSanchez:我还没有需要修改它。无论如何,这可能会有所帮助:github.com/Sephiroth87/ODRefreshControl/pull/…
      • @DanielSanchez aah,在ODRefreshControl.m 中修改#define kMaxDistance 53 参数。我想这会有所帮助
      • 您先生是个天才!非常感谢!我相信这是任何试图做我正在做的事情的人的解决方案。我刚刚使用ODRefreshControl 并修改了#define kMaxDistance 53 常量以获得我想要的输出。哇哦!
      • 伙计们,这个答案现在似乎是错误的。你可以像 Ashkan 解释的那样调用 .beginRefreshing()。
      猜你喜欢
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2019-11-12
      • 2014-03-30
      • 2022-06-15
      • 1970-01-01
      相关资源
      最近更新 更多