我试图覆盖原来的 wkwebview 操作表...
首先,我禁用了原来的 wkactionsheet webView.evaluateJavaScript("document.body.style.webkitTouchCallout='none';", completionHandler: nil)
然后我初始化一个长按手势识别器(它工作得很好)并创建了我自己的操作表。我使用了 decisionPolicyForNavigationAction 来获取点击的链接网址:
func onLongPress(gestureRecognizer:UIGestureRecognizer){
if gestureRecognizer.state == UIGestureRecognizerState.Began {
longPressSwitch = true
}
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if(navigationAction.navigationType == .LinkActivated) {
longPressAcUrl = navigationAction.request.URL!.absoluteString
if(longPressSwitch == true) {
let ac = actionMenu(self)
self.presentViewController(ac, animated: true) {
}
decisionHandler(.Cancel)
longPressSwitch = false
}
}
decisionHandler(.Allow)
}
问题是,操作表在手指释放后显示(即recogniser.state = .Ended),但我希望它像Chrome一样显示,这应该是用户按下链接后0.5秒或更短的时间......(即recogniser.state = .Begin),我该怎么办?
ps:这是我的行动表:
//Rebuild Wkactionsheet
func actionMenu(sender: UIViewController) -> UIAlertController {
let alertController = UIAlertController(title: "", message: longPressAcUrl, preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Open", style: .Default) { (action) in
//...
}
alertController.addAction(openAction)
let opentabAction = UIAlertAction(title: "Open in New Tab", style: .Default) { (action) in
//...
}
alertController.addAction(opentabAction)
let copyurlAction = UIAlertAction(title: "Copy Link URL", style: .Default) { (action) in
//...
}
alertController.addAction(copyurlAction)
return alertController
}
另外,如果我试图把
let ac = actionMenu(self)
self.presentViewController(ac, animated: true) {}
在 onLongPress() 处,虽然无法从 navigationAction.request.URL!.absoluteString 获取 URL (longPressAcUrl),但它工作正常!