2
votes

I have a very strange issue, UITextView touch event crash on double tap whereas same code works with < iOS10 version. (It means below iOS10 version there is no crash for press gesture recognizer)

Actually, I am adding the double tap and log press gesture based on permission. If the user has permission to comment then add gestures in viewDidLoad methods. Comment is allowed only with double tap or long press

singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureAction:)];
singleTapGesture.numberOfTapsRequired = 1;

// adding gesture to open window for commenting only when he has writing access

if (canComment) {

    longPressgesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
    longPressgesture.minimumPressDuration = 0.2;
    doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doDoubleTap:)];
    doubleTap.numberOfTapsRequired = 2;

}

On single tap

-(void)singleTapGestureAction:(UITapGestureRecognizer*)tapGestureRecognizer{

if (isSingleTapped) {
    isSingleTapped = NO;
    return;
}

isSingleTapped = YES;

UITextView *textView = (UITextView *)tapGestureRecognizer.view;

[self.commentView becomeFirstResponder]; // becomeFirstResponder
}

On double tap

-(void)doDoubleTap:(UITapGestureRecognizer*)tapGestureRecognizer
{
     UITextView *textView = (UITextView *)tapGestureRecognizer.view;
     [self.commentView becomeFirstResponder]; // becomeFirstResponder

     // To show the UIMenuController menu
     [self setCommentMenuToolTipWithRect:completeRect];
}

NOTE: I am adding [self.commentView becomeFirstResponder]; on every gesture action

UITextView delegate methods

- (void)textViewDidBeginEditing:(UITextView *)inView
{

    [self.commentView becomeFirstResponder];
    range=[self.commentView selectedRange];    
}

USE CASE:

When I double tap to select any word then APP CRASH and UIMenuController does not appear, enter image description here

but if I add the following line app does not crash

- (void)textViewDidChangeSelection:(UITextView *)textView{
    [textView resignFirstResponder];
}  // app does not crash

and UIMenuController appears with comment menu items that's great. I was happy that I have fixed the crash issue.

But there is another problem, when I press outside, menu hides and select any word AGAIN then It does not appear SECOND time.


I have tried all the possible way to show the menu for returns YES/TRUE to canBecomeFirstResponder. I know, there has to be a view that claims firstResponder for the menu to show. but how ?

On second time touch, not even calling any gesture recognizer method

2
All details below the USE CASE: header are confusing. Is your problem the crash or tool tip? Please update your question accordingly and remove all irrelevant information.Swapnil Luktuke
@lukya. First of all its crashing double tap on UITextView, but then if I add textViewDidChangeSelection then it does not crash but then tooltip does not appearPraveen-K
Thats impossible to solve just looking at your question. If possible, provide the source code (via github etc).Swapnil Luktuke
@lukya ok. I willPraveen-K
You should add swift tag instead of iPad or iPhone.Ramis

2 Answers

2
votes

From the logs it is clear that when double tap is recognized, same touch update is also sent to another gesture recognizer, which fails. So, a simple solution would be to avoid detection of other gestures on double tap. This can simply be achieved by making all other gestures on commentView require doubleTap to fail using requireGestureRecognizerToFail. just add the condition in addGestureToTextView method as shown below.

if (withDoubleTap && self.canScreenPlayEdit) {
    [self.commentView removeGestureRecognizer:singleTapGesture];
    [self.commentView addGestureRecognizer:doubleTap];
    [self.commentView addGestureRecognizer:longPressgesture];

    for (UIGestureRecognizer *recognizer in self.commentView.gestureRecognizers) {
        [recognizer requireGestureRecognizerToFail:doubleTap];
    }
}

This does solve the crash and also shows the menu without calling resignFirstResponder in textViewDidChangeSelection.

However, there seem to be many issues in your code. PLSceneDetailsVC is too complicated and you need to simplify the code. You need to streamline the gesture management or you will end up facing many more such issues.

1
votes
 longPressgesture.minimumPressDuration = 0.2;

My guess the problem is here. 0.2s is way too small to be used for longPress. Probably both were triggered (longPress and double tap).

Change it to higher like 1.5s.