如何将a添加UITapGestureRecognizer到UITextView,但仍然UITextView像 往常 一样通过触摸?
UITapGestureRecognizer
UITextView
当前,一旦我将自定义手势添加到textView中,它就会阻止对UITextView默认操作(如定位光标)的点击。
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行为(如光标定位)不变?
为此,使您的视图控制器采用UIGestureRecognizerDelegate,并且重写应与手势识别器方法同时识别,例如:
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 }