4

I have a problem in my gesture recognizers. My goal is to implement using swipe to delete in my table view. But I think other gestures are conflicting with each other. I'm using this libray romaonthego/REFrostedViewController this a library for my hamburger menu and this library has a pangesture feature. I think the conflict is w/in the gestures. because when I run my code of my tableview in another project it's working.Pls help, Thank you in advance.

4 Answers4

4

I had a similar problem, what I ended up doing is similar to TonyMkenu , but there are more recognizers that you need to allow:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

if (otherGestureRecognizer.delegate == self )
    return NO;

//if otherGestureRecognizer is swipe to delete from a UITableView cancel slide menu recognizers

if ([[otherGestureRecognizer.view class] isSubclassOfClass:[UITableView class]])
{
    NSLog(@"Allow1 %@", [otherGestureRecognizer description]);

    return YES;
}

if( [[otherGestureRecognizer.view class] isSubclassOfClass:[UITableViewCell class]] ||
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewCellScrollView"] ||
[NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewWrapperView"])
{
    NSLog(@"Allow&Disable %@", [otherGestureRecognizer description]);

    if(gestureRecognizer.delegate == self)
    {//cancel the slide menu recognizer

        gestureRecognizer.enabled = NO;
        gestureRecognizer.enabled = YES;
    }

    return YES;
}

NSLog(@"Deny %@", [otherGestureRecognizer description]);
return NO;

}

Kamen Dobrev
  • 1,321
  • 15
  • 20
4

Edit: Updated for iOS 11

The other answers were helpful, but in my case the best solution was to do the logic in shouldRequireFailureOfOtherGesture like so:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (gestureRecognizer == self.pan) {
        return YES;
    }
    return NO;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (gestureRecognizer == self.pan) {

        // iOS 10
        if ([NSStringFromClass([otherGestureRecognizer.view class]) isEqualToString:@"UITableViewWrapperView"]) {
            return YES;
        }
        // iOS 11
        else if ([otherGestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer.view isKindOfClass:[UITableView class]]) {
            return YES;
        }
    }
    return NO;
}

This had a much better behavior in my case. I also used delaysTouchesBegan = YES on my pan gesture. Might be useful!

beebcon
  • 6,893
  • 5
  • 25
  • 27
2

In iOS 11,hope this can help you.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([[otherGestureRecognizer.view class] isSubclassOfClass:[UITableView class]]) {
        if ([otherGestureRecognizer isKindOfClass: [UIPanGestureRecognizer class]]) {
            UIPanGestureRecognizer *otherPan = (UIPanGestureRecognizer *)otherGestureRecognizer;
            CGPoint translation = [otherPan translationInView:otherGestureRecognizer.view];
            return translation.x < 0;
        }
    }
    return NO;
}
0

First of all... check if you have this

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

and second...

try to add this

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    if ([gestureRecognizer.view isKindOfClass:[UITableView class]]) {
       return YES;
    } else {
       return NO;
    }
}

https://stackoverflow.com/a/14338043

Community
  • 1
  • 1
TonyMkenu
  • 7,597
  • 3
  • 27
  • 49
  • So... any news from you? – TonyMkenu Sep 04 '14 at 10:00
  • Sorry for the late reply, I already solve my problem by putting a reference to the gesture of my hamburger menu. Thank you for your answer :) – Alvin John Tandoc Oct 09 '14 at 01:44
  • @AlvinJohnTandoc I am facing similar problem in my current project. I assume that solution provided by TonyMkenu worked for you. If my assumption is correct, can you please tell me where to put the code (function provided in the answer)? Right now my code doesn't use these function anywhere. So should I put it in menu classes or in my own view controller classes? – Dipak Mishra May 29 '15 at 12:47