在 WebView 中拉动刷新 Swift 5

Pull to refresh in WebView Swift 5

尝试实现刷新 WebView 的选项URL但没有成功,到目前为止我有这个

class ViewController2: UIViewController {

@IBOutlet var webview2: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    webview2.load(URLRequest(url: URL(string: "https://coinmarketcap.com/")!))
    
}


//Pull to Refresh
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

     if (scrollView.contentOffset.y < 0){
     //reach top
          print("Reach Top")
          webview2.reload()
     }
 }   
}

谁能告诉我为什么它不起作用?我是新手

在意识到自己犯了一些错误之后,我找到了解决办法。刷新 WebView 的正确代码是:

class ViewController2: UIViewController {

@IBOutlet var webview2: WKWebView!
@objc let refreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()
    webview2.load(URLRequest(url: URL(string: "https://coinmarketcap.com/")!))
    
    webview2.scrollView.bounces = true
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(ViewController2.refreshWebView), for: UIControl.Event.valueChanged)
    webview2.scrollView.addSubview(refreshControl)
}

@objc func refreshWebView(sender: UIRefreshControl) {
    webview2.load(URLRequest(url: URL(string: "https://coinmarketcap.com/")!))
    sender.endRefreshing()
}
}