如何检查刷新控件是否在其目标操作方法中刷新

How to check if the refresh control is or isn't refreshing inside its target action method

我有一个 collectionView 和一个 refreshControl

lazy var refreshControl: UIRefreshControl = {
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self,
                             action: #selector(handleRefreshControl(_:)),
                             for: .valueChanged)
    return refreshControl
}()

//create collectionView
collectionView.refreshControl = refreshControl

我按下按钮从服务器获取一些数据。该过程开始后,我使用 refreshControl.refreshManually() 开始刷新。

getDataBarButton() {

    refreshControl.refreshManually()

    fetchData()
}

检索到数据后,我调用 refreshControl.endRefreshing() 停止刷新。

func fetchData() {

    // got the data

    refreshControl.endRefreshing()
}

一切正常。这不是如果从数据库中提取更多数据的情况,因为只有按钮可以做到这一点。拉出简历后,我想显示 refreshControl 然后将其删除。我不想在完成后完全摆脱 refreshControl,因为按钮可能会再次按下。

问题是一旦我拉出简历,refreshControl 开始刷新并且 不会停止 。它只是停留在那里。

我检查它是否停止了,但这不起作用:

@objc fileprivate func handleRefreshControl(_ sender: UIRefreshControl) {
    if refreshControl.isRefreshing {
        print("refreshing is occurring")
        return
    }
    refreshControl.refreshManually()
    refreshControl.endRefreshing()
}

我的看法是,在返回数据并调用 after refreshControl.endRefreshing() 之后,一旦我将 cv 中的打印语句拉到 if refreshControl.isRefreshing 不应该 运行但是它会

除了创建和切换像 var isRefreshing = false/true 这样的变量之外,如何检查 refreshControl 是否在其 target action 中刷新?

仅供参考,如果您正在 stack overflow iOS 应用程序上阅读此问题,请下拉并观察会发生什么。没有什么可刷新的,但是当拉动发生时,会显示一个刷新控件,然后将其删除。我想做同样的事情。

除了使用 属性

我找不到任何其他方法
var isRefreshing = false // 1. initially set to false

func startRefreshing() {

    isRefreshing = true // 2. create a function that sets isRefreshing to true and also calls .refreshManually
    refreshControl.refreshManually()
}

func endRefreshing() {
    isRefreshing = false  //  3. create a function that sets isRefreshing to false and also calls .endRefreshing
    refreshControl.endRefreshing()
}

getDataBarButton() {

    startRefreshing() // 4. when then button is pressed call the function to start refreshing
    fetchData()
}

func fetchData() {

    // got the data
    endRefreshing()  // 5. after the data is returned call the function to endRefreshing
}

func handleRefreshControl(_ sender: UIRefreshControl) {

    // 6. in the refresher's target action check to see if isRefreshing == false and if it is then call refreshControl.endRefreshing()
    if !isRefreshing {

        refreshControl.endRefreshing()
    }
}