小编典典

将UITapGestureRecognizer添加到UITextView而不阻止textView触摸

swift

如何将a添加UITapGestureRecognizer到UITextView,但仍然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行为(如光标定位)不变?


阅读 382

收藏
2020-07-07

共1个答案

小编典典

为此,使您的视图控制器采用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
}
2020-07-07