我有一个UIWebView,其中我实现了一个单击手势识别器。虽然我的问题在于选择,但我没有长时间保持识别器。的问题:
...
func createGestureRecognizer() {
// single tap
let singleTapGesture = UITapGestureRecognizer(target: self, action: "handleSingleTap:")
singleTapGesture.numberOfTapsRequired = 1
singleTapGesture.delegate = self
singleTapGesture.cancelsTouchesInView = false
webView.addGestureRecognizer(singleTapGesture)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if touch.view.isDescendantOfView(self.webView) {
return true
}
return true
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if otherGestureRecognizer.isKindOfClass(UITapGestureRecognizer) {
otherGestureRecognizer.requireGestureRecognizerToFail(gestureRecognizer)
// println("added failure requirement to: \(otherGestureRecognizer)")
}
return true
}
func handleSingleTap(tap: UITapGestureRecognizer) {
…
}
编辑:有点工作。问题:进行初始文本选择需要比平时更长的保持时间。通常进行选择的正常长保持会调用我的单击识别器而不选择任何文本。
func handleSingleTap(tap: UITapGestureRecognizer) {
let selection = webView.stringByEvaluatingJavaScriptFromString("window.getSelection().toString()")!
if selection == "" {
// do stuff
} else {
webView.userInteractionEnabled = false
webView.userInteractionEnabled = true
webView.stringByEvaluatingJavaScriptFromString("window.getSelection().removeAllRanges()")
}