0

I need to apply a function when the user touches the screen with two fingers. My problem that is the screen has a UITextView.

There is also active keyboard present on the screen, that's why the usual approach (UITapGestureRecognizer) doesn't work for this configuration.

How can I do this?

Thanks.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
benhi
  • 574
  • 1
  • 6
  • 24

3 Answers3

3

You need to add a UITapGestureRecognizer to the UITextView.

Here's what you must do:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(respondToTap)];

[singleTap setNumberOfTapsRequired:2];
[self.yourTextView addGestureRecognizer:tapRecognizer];


-(void)respondToTap{
//do whatever you have to when textfield is double tapped
}
  • Thanks for your answer but it doesn't work, I already tried this method. The problem is that I think UITapGestureRecognizer isn't supported by UITextView. – benhi Feb 19 '12 at 16:05
  • But that was with only one tap, so it may not work with more than one. Sorry for the confusion. –  Feb 19 '12 at 16:20
  • Ok I understood the problem, when the keyboard is up and the cursor is available, with this method we can't have a UITextView respond to touches even for one tap. And in my case the keyboard and the cursor are always available. So I need to find solution for this configuration. Maybe you know another way?? Thanks. – benhi Feb 20 '12 at 07:48
  • Try setting the `UITextView` to not editable. Maybe this might help, but probably not. –  Feb 20 '12 at 13:53
1

Since you can't subclass UIKeyboard to pass touches maybe this link will help you.

Detect if the user has touched the screen

There's a subclass of UIWindow - if you set it as your main application window (in appDelegate) you might catch touches before they get to keyboard. There you should be able to check if touches are in the UITextField area (and handle them properly) or send them down to responder chain.

Not as elegant as using gesture recognisers but you could make it work.

Community
  • 1
  • 1
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
0

Similar to this answer I gave to a similar question, but with the caveat that I've only used this on iOS 10, you can do this by extending UITextView with a custom class and overriding addGestureRecognizer:.

Keeping track of the singleTap just acts as a sentinel for adding the two finger tap, since the UITextView is constantly adding and dropping gestures:

@interface MyCustomTextView ()

@property (weak, nonatomic) UITapGestureRecognizer *singleTap;

@end


@implementation MyCustomTextView

/**
 *  this will fire when the text view is tapped with two fingers
 *
 *  @param tgr
 */
- (void)_handleTwoTouches:(UITapGestureRecognizer *)tgr
{
    // ADD CODE HERE
}

- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    [super addGestureRecognizer:gestureRecognizer];
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
        UITapGestureRecognizer *tgr = (UITapGestureRecognizer *)gestureRecognizer;
        if ([tgr numberOfTapsRequired] == 1 && [tgr numberOfTouchesRequired] == 1) {

            if (!self.singleTap) {
                self.singleTap = tgr;

                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_handleTwoTouches:)];
                tap.numberOfTapsRequired = 1;
                tap.numberOfTouchesRequired = 2;
                [super addGestureRecognizer:tap];

            }

        }
    }
}

@end
Community
  • 1
  • 1
Jaymon
  • 5,363
  • 3
  • 34
  • 34