问题描述
如何将 UITapGestureRecognizer
添加到UITextView,但仍然可以触及到 UITextView
normal ?
How can I add a UITapGestureRecognizer
to a UITextView but still have the touches getting through to the UITextView
as normal?
目前,只要我向textView添加自定义手势,它就会阻止点击UITextView默认操作,例如定位光标。
Currently as soon as I add a custom gesture to my textView it blocks the tap for UITextView default actions like positioning the cursor.
var tapTerm:UITapGestureRecognizer = UITapGestureRecognizer()
override func viewDidLoad() {
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
textView.addGestureRecognizer(tapTerm)
}
func tapTextView(sender:UITapGestureRecognizer) {
println("tapped term – but blocking the tap for textView :-/")
…
}
我如何处理点击但保持像光标定位一样的textView行为?
How can I process taps but keep any textView behaviour like cursor positioning as is?
推荐答案
要做到这一点,视图控制器采用UIGestureRecognizerDelegate并覆盖shoul d同时识别手势识别器方法,如:
To do that make your view controller adopt to UIGestureRecognizerDelegate and override should recognize simultaneously with gesture recognizer method like:
override func viewDidLoad() {
tapTerm = UITapGestureRecognizer(target: self, action: "tapTextView:")
tapTerm.delegate = self
textView.addGestureRecognizer(tapTerm)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
这篇关于将UITapGestureRecognizer添加到UITextView而不阻止textView触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!