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

时间:2014-08-27 11:37:40

标签: ios xcode uitableview uipangesturerecognizer

我的手势识别器有问题。我的目标是在我的表视图中使用滑动来删除。但我认为其他手势相互冲突。我正在使用这个图书馆romaonthego/REFrostedViewController 这个图书馆为我的汉堡菜单,这个库有一个pangesture功能。我认为冲突是在手势中。因为当我在另一个项目中运行我的tableview代码时,它正在工作。请帮助,提前谢谢你。

4 个答案:

答案 0 :(得分:4)

编辑:针对iOS 11更新

其他答案很有帮助,但在我的情况下,最好的解决方案是在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;
}

在我的案例中,这有一个更好的行为。我也在我的平移手势上使用了delaysTouchesBegan = YES。可能有用!

答案 1 :(得分:3)

我有类似的问题,我最终做的与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;

}

答案 2 :(得分:1)

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

答案 3 :(得分:0)

首先......检查你是否有这个

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

和第二......

尝试添加此

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

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

https://stackoverflow.com/a/14338043