我想向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)

最佳答案

delegatesingleCodeTextViewTap设置为您的视图控制器,实现gestureRecognizerShouldBegin:,如果falsesaveDrawerOut,则返回true

10-07 23:31