向UITextView添加手势会取消默认行为

时间:2015-04-01 14:32:59

标签: ios objective-c iphone swift

我想向UITextView添加单击手势,以便在我的保存对话框打开后,我可以点击UITextView取消它。但是,当我这样做时,它取消了textview的正常单击行为。现在,当我单击文本而没有保存抽屉时,它什么也没做。点击确实会使用回调方法正确触发,但会取消UITextView的默认操作,(将光标放在您点击的任何位置)。

// Add gesture to cancel saving
let singleCodeTextViewTap = UITapGestureRecognizer(target: self, action: Selector("codeTextViewTapped"))
singleCodeTextViewTap.numberOfTapsRequired = 1;
codeTextView.addGestureRecognizer(singleCodeTextViewTap)

func codeTextViewTapped() {
    if saveDrawerOut {
        moveFilenameBannerOffScreen()
        saveAsTextField.text = ""
    }
}

如何在单击手势的同时启用UITextView的默认行为?

修改
我尝试添加cancelsTouchesInView = false但这似乎不起作用,虽然它看起来像是正确的答案。所以现在我的代码看起来像这样:

singleCodeTextViewTap = UITapGestureRecognizer(target: self, action: Selector("codeTextViewTapped"))
singleCodeTextViewTap.numberOfTapsRequired = 1
singleCodeTextViewTap.cancelsTouchesInView = false
codeTextView.addGestureRecognizer(singleCodeTextViewTap)

3 个答案:

答案 0 :(得分:1)

delegate的{​​{1}}设置为您的视图控制器,实施singleCodeTextViewTap并在gestureRecognizerShouldBegin:false时返回saveDrawerOut

答案 1 :(得分:1)

您也可以将cancelsTouchesInView设置为false。所以它会是

singleCodeTextViewTap.cancelsTouchesInView = false

这样可以识别手势并将其转发到UITextView

答案 2 :(得分:1)

尝试singleCodeTextViewTap.cancelsTouchesInView = false

它应该让多个手势识别器相互协调。