UIGestureRecognizer and UITextView

I am working on a UITextView implementation in which I only want to respond to touches that are in a specific part of the text view.

I have a gesture recognizer attached to the view, and this works fine until I set the view as the first responder, what will I do if the transition point in the view is greater than the value of X and Y.

- (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 that after she’s set up on the first responder and then resigned by clicking outside this text view, my gesture recognition method is no longer called. If I touch the area that the first responder does not set, my method is called as many times as I click. If I install and then fire the first defendant, he does not respond after the first refusal.

- (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 a simple attempt, if I add a new gesture recognizer after each dismissal, then this works, but obviously is not a good solution.

Any thoughts?

+3
source share
2 answers

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

add this to your code

myGestuerRecognizer.delegate = self

then implements 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;  }

it works for me

+5
source

iOS 10 Swift 3.0

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

All Articles