ios - 在同一个 UIView 子类上用一根手指滑动和两根手指滑动

标签 ios uitableview uigesturerecognizer uipangesturerecognizer

我有 UITableViewCell 的自定义实现。 UITableViewCell 可以向左或向右滑动。 我使用 UIPanGestureRecognizer 来实现相同的目的。

UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        recognizer.delegate = self;
        [self addGestureRecognizer:recognizer];

           }

#pragma mark - horizontal pan gesture methods
-(BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer translationInView:[self superview]];
    // Check for horizontal gesture
    if (fabsf(translation.x) > fabsf(translation.y)) {
        return YES;
     }
    return NO;
}

-(void)handlePan:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
    // if the gesture has just started, record the current centre location
        // SOME CODE
    }

    if (recognizer.state == UIGestureRecognizerStateChanged) {
        // translate the center
        //SOME CODE
    }

    if (recognizer.state == UIGestureRecognizerStateEnded) {
        // the frame this cell would have had before being dragged
        //SOME CODE
    }
}

现在我希望能够支持在整个屏幕上两根手指滑动,这样即使在 UITableViewCell 上完成两根手指滑动,也不会触发上述代码。 我怎样才能实现这个目标?

最佳答案

如果您将手势识别器上的 maximumNumberOfTouches 设置为 1,它将不再接受多指滑动:

UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.maximumNumberOfTouches = 1;

关于ios - 在同一个 UIView 子类上用一根手指滑动和两根手指滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16995575/

相关文章:

iphone - 在 iOS Safari 上更改方向时出现白色闪烁

iOS:架构 i386 的 undefined symbol

ios - 在 iOS 7/Objective-C 中,如何以编程方式将 UIPickerView 捕捉到它所在的父 View 的底部?

ios - Tableview 圆角未显示在底部

ios - 无法使用 '[Array<String>]' 类型的索引为 '(section)' 类型的值添加下标。类型'(又名 'section.Type' )

uigesturerecognizer - 检测 UILabel 上的触摸事件

ios - 如何从 UITableView 单元格获取数据到全局变量

ios - 如何支持64位iOS并通过App Store?

ios - 单元格或标题 View 高度的 UITableViewAutomaticDimension 导致 UITableView insertRowsAtIndexPaths() 崩溃

objective-c - 使用 UIPanGestureRecognizer 围绕某个点旋转 UIView