Bryan Chen
Bryan Chen

Reputation: 46598

ios - UITapGestureRecognizer with UITextView

I have a scrollable, non-ediable UITextView. I want to add a gesture recognizer to it so when double tap it, a tool bar will show, double tap it again, tool bar will hide.

I have disabled the selection function of the text view by subclassing it and override canBecomeFirstResponder to return NO.

It seems ok when i just simply add the tap recognizer to it.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showOrHideToolbars)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[textView addGestureRecognizer:tapRecognizer];

It works good, excepts if i tap and hold on the text view, after that, the recognizer will not receive any action any more.

This means, if I tap and hold on the text view (i guess text view goes into a selection mode even i disable the selection function), no more double tap can be detected now.

I tried to use single tap then problem is gone but i do need to use double tap.

I also tried to override touch event handler methods, but no use.

Upvotes: 1

Views: 8020

Answers (2)

yunas
yunas

Reputation: 4163

Thanks... I managed it via Deepdak's suggestion... The following code will do the required thing :)

UITapGestureRecognizer *taprecog = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openFolderController)];
taprecog.numberOfTapsRequired = 2;
taprecog.numberOfTouchesRequired = 1;

NSMutableArray *arr = [[NSMutableArray alloc]initWithArray:[textView gestureRecognizers]];
for (int i = 0; i < [arr count]; i++) {
    if ([[arr objectAtIndex:i] isKindOfClass:[UITapGestureRecognizer class] ]) {
        [arr removeObject:[arr objectAtIndex:i]];
    }   
}

[textView addGestureRecognizer:taprecog];
[taprecog release];

Upvotes: 1

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

There are a number of other gesture recognizers attached to a text view. Since you don't seem to need them. You can remove them.

textView.gestureRecognizers = nil;

before adding your double tap recognizer. It works.

Upvotes: 7

Related Questions