4
votes

I'm working on implementing a UITextView in which I only want to respond to touches that are in a specified part of the text view.

I have a gesture recognizer attached to the view, and that works fine, until I set the view to become first responder, which I do if the tap's point in the view is greater then an X an Y value.

- (IBAction)textViewTapped:(UIGestureRecognizer *)sender {
CGPoint point = [sender locationOfTouch:0 inView:self.view];

NSLog(@"x ix %f, y is %f", point.x, point.y);

if (point.x > 96 && point.y > 106)
    [self.myTextView becomeFirstResponder];
}

The problem is, once it is set to be first responder, and then resigned by tapping outside of that text view, my gesture recognizer method is never called again. If I tap in the area that doesn't set first responder, then my method gets called as many times as I tap. If I set and then resign first responder, it doesn't respond after the first time it is resigned.

- (IBAction)viewTapped:(UIGestureRecognizer *)sender {
    [self.view endEditing:YES];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textViewTapped:)];
    [self.myTextView addGestureRecognizer:tap];
    NSArray *gestures = [self.myTextView gestureRecognizers];
    NSLog(@"got %d recognizers", [gestures count]);  
}

In just trying things, if I add a new gesture recognizer after each resign, then that works, but is obviously not a good solution.

Any thoughts?

2

2 Answers

5
votes

I have the same problem, and i solved it by implementing dummy UIGestureRecognizerDelegate

add this to your code

myGestuerRecognizer.delegate = self

then implement UIGestureRecognizerDelegate

#pragma mark - UIGestureRecognizerDelegate

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;  }

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer
*)otherGestureRecognizer{
    return YES;  }

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    return YES;  }

this works for me

0
votes

iOS 10 Swift 3.0

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {    
  return true
}