How to add double gestures to a UITextView

Currently, I want a UITextView to have a double click gesture. UITableView seems to have its own double-touch gesture, when we double-tap, some text will be selected. Therefore, I want to delete this double double-tap gesture on my own gesture recognizer. I tried many methods and it all failed. There seems to be no way to remove the default resolver of a UITextView. I also want to add a transparent view to this UITextView to make a double-click event, but this sub-item will block other gestures in the UITextView. Is there a way to add a dual gesture recognizer to a UITextView? I really hope that there will be work.

I'm still waiting for iOS5 :)

+4
source share
3 answers

I have a solution on iOS6, we can use UIGestureRecognizerDelegate and override gestureRecognizerShouldBegin: and gestureRecognizer: shouldReceiveTouch :. In these two methods, we can check if the gesture is doubleTapGestureForZooming, if you do not return NO or return YES. This works fine in iOS6, but in iOS5 these two delegate methods were not called, so iOS5 might need another workaround. Finally, I get a workaround, we can override the addGestureRecognizer UITextView method to remove the default gesture, wanting it to help someone else.

PS: we really cannot delete UITextView system gestures, we cannot even change their property. It seems that when the event happens, all the UItextview gestures will be added again.

+2
source

There are many other gesture recognizers attached to the text view. Since you do not seem to need them. You can delete them.

myTextView.gestureRecognizers = nil; 

before adding a double tap recognizer. He works.

then you can add

 UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mySelector)]; tapRecognizer.numberOfTapsRequired = 2; tapRecognizer.numberOfTouchesRequired = 1; [myTextView addGestureRecognizer:tapRecognizer]; 
+13
source

I know this question is out of date, but to keep it relevant for future search engines, I thought I would add another solution that worked for me from iOS 7 to 10. It basically combines the solutions discussed here and here , but modifies them, to get a UITextView for recognizing a user double click.

He does this by subclassing the UITextView and overriding the addGestureRecognizer: method to embed our custom callback in double-touch gestures and set up a single-touch gesture to respect the new double-tap.

I do this in addGestureRecognizer: because the UITextView constantly deletes and adds gestures depending on its current state, and therefore you constantly have to copy reset.

This code should be enough for someone to start:

 @interface MyCustomTextView () /** * we want to keep track of the current single-tap gesture so we can make sure * it waits for a double-tap gesture to fail before firing */ @property (weak, nonatomic) UITapGestureRecognizer *singleTap; /** * we want to keep track of the current double-tap gesture so we can tell a single * tap gesture to ignore this double-tap when the single tap gesture changes */ @property (weak, nonatomic) UITapGestureRecognizer *doubleTap; @end @implementation MyCustomTextView /** * this will fire when the text view is double-tapped * * @param tgr */ - (void)_handleTwoTaps:(UITapGestureRecognizer *)tgr { // ADD CODE HERE } /** * the reason why I've overridden this methods is these gestures change quite a bit * depending on the state of the UITextView, (eg, when in focus and out of focus) * and so this provides a reliable way to make sure any new gestures get updated * with custom overrides. * * @param gestureRecognizer */ - (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { [super addGestureRecognizer:gestureRecognizer]; if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { UITapGestureRecognizer *tgr = (UITapGestureRecognizer *)gestureRecognizer; if ([tgr numberOfTapsRequired] == 1 && [tgr numberOfTouchesRequired] == 1) { self.singleTap = tgr; if (self.doubleTap) { [tgr requireGestureRecognizerToFail:self.doubleTap]; } } else if ([tgr numberOfTapsRequired] == 2 && [tgr numberOfTouchesRequired] == 1) { [tgr addTarget:self action:@selector(_handleTwoTaps:)]; self.doubleTap = tgr; if (self.singleTap) { [self.singleTap requireGestureRecognizerToFail:tgr]; } } } } // NOTE: I'm not sure if this is needed but it been there for years // and so I thought I would include it just in case - (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) { UITapGestureRecognizer *tgr = (UITapGestureRecognizer *)gestureRecognizer; if ([tgr numberOfTapsRequired] == 2 && [tgr numberOfTouchesRequired] == 1) { [tgr removeTarget:self action:@selector(_handleTwoTaps:)]; } } [super removeGestureRecognizer:gestureRecognizer]; } @end 
+1
source

All Articles