我需要在WKWebView中添加UIGestureRecognizer。
我将轻击手势设置为WKWebView,它可以正常工作,直到加载网页。 当网页加载完成后,它就无法识别手势了。
有没有什么方法可以在WKWebView中添加手势?
import UIKit
import WebKit
class SwipeObserveExperimentController: UIViewController {
let webView: WKWebView = {
let v = WKWebView()
v.translatesAutoresizingMaskIntoConstraints = false
v.scrollView.backgroundColor = .green
return v
}()
override func viewDidLoad() {
super.viewDidLoad()
// Layout
view.addSubview( webView )
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor .constraint(equalTo: view.topAnchor ).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor ).isActive = true
webView.rightAnchor .constraint(equalTo: view.rightAnchor ).isActive = true
webView.leftAnchor .constraint(equalTo: view.leftAnchor ).isActive = true
// Gesture
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTap) )
webView.addGestureRecognizer(tapGesture)
// Load URL
let url = URL(string: "http://www.akr-ski.com/")!
let request = URLRequest(url: url)
webView.load(request)
}
func viewTap(gesture: UITapGestureRecognizer) {
print("View Tap")
}
}