[swift]将 UITapGestureRecognizer 添加到 UITextView,而不会阻止 textView 触动

标签: UITextView ios Swift
发布时间: 2015/6/17 23:39:35
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

如何添加 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 :-/")
…
}

我如何可以处理tap但保持像光标定位正如任何 textView 行为吗?

解决方法 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
}
官方微信
官方QQ群
31647020