Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept long press on UITextView?

Total Objective-C / Cocoa Touch noob here, beware.

I'm trying to intercept when a user long presses on a UITextView (a magnifying glass then appears with the caret positioner) and then releases the touch, i.e. when normally the "Select" and "Select All" Options appear, after the magnifying glass. I want to replace this with my own custom action that is then performed.

Is this possible?

like image 994
Epaga Avatar asked Oct 08 '10 18:10

Epaga


1 Answers

You can try something like this:

Disable the built-in long press recognizer

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

Then add your own

UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:<your target> action:@selector(<your custom handler>)]; 
[textView addGestureRecognizer:myLongPressRecognizer];
[myLongPressRecognizer release];
like image 93
Altealice Avatar answered Sep 20 '22 11:09

Altealice