how to completely remove gesture recognizers

10,868

Why don't you use the below gesture delegate to stop any gesture:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
Share:
10,868
EarlGrey
Author by

EarlGrey

Swift now, Objective-C before, DevOps way before. iOS, macOS, watchOS, tvOS.

Updated on June 04, 2022

Comments

  • EarlGrey
    EarlGrey almost 2 years

    I'm trying to remove three gesture recognizers attached to a uiscrollview.

    I list them using

    NSArray * activeScrollViewGRecs = [theScrollView gestureRecognizers];
    NSLog (@"activeScrollViewGRecs count: %d",[activeScrollViewGRecs count]);
    

    I get the three listed.

    Then I remove them with:

    for (UIGestureRecognizer *recognizer in activeScrollViewGRecs)
    {
        NSLog (@"recognizer: %@",recognizer.description);
        recognizer.enabled = NO;
        [theScrollView removeGestureRecognizer:recognizer];
    }
    

    Then I list them again, and get a zero count. They should be gone/removed, right ? Why would then the view continue to respond (and gesture methods getting called) to the same touches/swipes. Is there some kind of a "flushing" mechanism that needs to happen before they're gone for good ?

    this is how they get created:

    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handle1:)];
    tapGesture.cancelsTouchesInView = NO; tapGesture.delaysTouchesEnded = NO; 
    tapGesture.numberOfTouchesRequired = 2; tapGesture.numberOfTapsRequired = 2;     
    [self.view addGestureRecognizer:tapGesture]; [tapGesture release];
    
    swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handle2:)];
    swipeGesture.cancelsTouchesInView = NO; swipeGesture.delaysTouchesEnded = NO; swipeGesture.delegate = self;
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeGesture]; [swipeGesture release];
    

    thanks

  • EarlGrey
    EarlGrey about 12 years
    Hi. I am removing them from the same view that they're attached to. I confirm this by checking the gestureRecognizers array before and after the removal. It's 3 before and 0 after.
  • EarlGrey
    EarlGrey about 12 years
    this is the method I'm removing them in. Now I think that might be THE problem. Removing them in the middle of a delegate method.
  • cocoakomali
    cocoakomali about 12 years
    Why do you need to remove it? Cant you just return NO in this function?
  • EarlGrey
    EarlGrey about 12 years
    I guess I'll have to, since I am not being able remove them properly.
  • cocoakomali
    cocoakomali about 12 years
    You can try one more thing. Do not release the gesture recognizer object after adding it. Use the same while removing it in the function - [removeGestureRecognizer:]
  • Philippe Sabourin
    Philippe Sabourin about 12 years
    Scrollviews always have gesture recognizers, but the view you're adding to doesn't seem to be the one you're removing from, according to the code you've posted. Try doing it to self.view instead of theScrollView and see if that fixes your issue.