简体   繁体   English

UITableView上的手势冲突滑动以删除iOS

[英]Gestures Conflict on UITableView swipe to delete iOS

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. 我正在使用这个libray romaonthego / REFrostedViewController这个我的汉堡包菜单的库,这个库有一个pangesture功能。 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. 因为当我在另一个项目中运行我的tableview代码时,它正在工作。请帮助,提前谢谢你。

I had a similar problem, what I ended up doing is similar to TonyMkenu , but there are more recognizers that you need to allow: 我有一个类似的问题,我最终做的是类似于TonyMkenu,但你需要允许更多的识别器:

- (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;

} }

Edit: Updated for iOS 11 编辑:已针对iOS 11进行了更新

The other answers were helpful, but in my case the best solution was to do the logic in shouldRequireFailureOfOtherGesture like so: 其他答案很有帮助,但在我的情况下,最好的解决方案是在shouldRequireFailureOfOtherGesture执行逻辑, shouldRequireFailureOfOtherGesture所示:

- (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. 我在平移手势上也使用了delaysTouchesBegan = YES Might be useful! 可能有用!

In iOS 11,hope this can help you. 在iOS 11中,希望这可以帮助您。

- (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;
}

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 https://stackoverflow.com/a/14338043

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM