微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将UITapGestureRecognizer添加到UITextView而不阻止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
}

解决方法

如何将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行为(如光标定位)不变?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。