How to customize UIRefreshControl to make pull-dow

2019-06-20 18:53发布

In UIRefreshControl in iOS App, I think the default setting is that when I pull down UITableView(UISrollView) by about "100px", refresh begins.

I would like to make the above value smaller.(for example, "50px")

Is this possible?

If possible, please tell me sample codes.

2条回答
聊天终结者
2楼-- · 2019-06-20 19:38

you can make it right with some modification

lazy var refreshControl: UIRefreshControl = {
        let refreshControl = UIRefreshControl()
        refreshControl.tintColor = UIColor.red
        return refreshControl
    }()

//refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)

every PullToRefresh must have couple lines of code like this, that handleRefresh function, do whatever you need to refresh the page.

you just need to comment out addTarget line and add this function to your code ```

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y < -80 { //change 80 to whatever you want
            if  !self.refreshControl.isRefreshing {
                handleRefresh()
            }
        }
    }
查看更多
干净又极端
3楼-- · 2019-06-20 19:41

Try this:

// definition
extension UIRefreshControl {
    func refreshManually() {
        beginRefreshing()
        sendActions(for: .valueChanged)
    }
}

// usage
var isRefreshingManually = false
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y < -80.0 {
        if !isRefreshingManually && !refreshControl.isRefreshing {
            isRefreshingManually = true
            refreshControl.refreshManually()
        }
    } else if scrollView.contentOffset.y >= 0 {
        isRefreshingManually = false
    }
}

My sample code is for UICollectionView, but UITableView & UIScollView work well, too.

Replace "-80.0" in my code to threshould value you want.

查看更多
登录 后发表回答