如何拦截UITextView上的长按?

时间:2010-10-08 18:42:52

标签: iphone objective-c cocoa-touch uitextview

Total Objective-C / Cocoa Touch noob,请注意。

当用户长时间按下UITextView(放大镜然后与插入符号定位器一起显示)然后释放触摸时,我试图拦截,即通常出现“选择”和“全选”选项后,放大镜。我想用我自己的自定义操作替换它,然后执行。

这可能吗?

3 个答案:

答案 0 :(得分:14)

您可以尝试这样的事情:

禁用内置长按识别器

for (UIGestureRecognizer *recognizer in textView.gestureRecognizers) {
  if ([recognizer isKindOfClass:[UILongPressGestureRecognizer class]]){
    recognizer.enabled = NO;
  }
}

然后添加自己的

UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:<your target> action:@selector(<your custom handler>)]; 
[textView addGestureRecognizer:myLongPressRecognizer];
[myLongPressRecognizer release];

答案 1 :(得分:2)

@ Altealice的Swift版本禁用内置长按识别器的代码:

if let actualRecognizers = self.sourcesTextView.gestureRecognizers {
    for recognizer in actualRecognizers {
        if recognizer.isKindOfClass(UILongPressGestureRecognizer) {
            recognizer.enabled = false
        }
    }
}

此解决方案有效,但要注意它将禁用textView交互,因此按下时链接不会突出显示,并且文本将无法选择。

答案 2 :(得分:0)

如果删除- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 它将起作用..因为将调用标签手势以开始编辑textField ...或者只是实现此手势委托功能

A