当我下拉刷新我的 UIScrollView 时,它需要我下拉到屏幕中间

When I pull down to refresh on my UIScrollView it requires me to pull down to the middle of the screen

我有一个函数:didPullToRefresh()。为了启动此功能,我必须将 UIScrollView 一直拉到屏幕中间。有没有一种方法可以通过仅将屏幕下拉 50 像素来刷新我想刷新的数据?谢谢?

这是我目前的代码 运行:

var refreshControl: UIRefreshControl!
override func viewDidLoad()  {
   super.viewDidLoad()

   scrollView.alwaysBounceVertical = true
   scrollView.bounces  = true
   refreshControl = UIRefreshControl()
   refreshControl.addTarget(self, action: #selector(didPullToRefresh), for: .valueChanged)
   self.scrollView.addSubview(refreshControl)
 }

@objc func didPullToRefresh() {

 print("Refersh")

 // For End refrshing
refreshControl?.endRefreshing()  


 }

不要将目标添加到刷新控件。只需将它的 addSubView 添加到 scrollview 即可。在 viewDidLoad:

scrollView.alwaysBounceVertical = true
scrollView.bounces  = true
scrollView.delegate = self

refreshControl = UIRefreshControl()
self.scrollView.addSubview(refreshControl)

将滚动视图委托设置为自己。并实现以下滚动视图委托方法:

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    if scrollView.contentOffset.y <= -50 {
        refreshControl.beginRefreshing()
        print("Start Refresh Service")
    }
}

在刷新服务完成时写入 "refreshControl?.endRefreshing()"。