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



我有一个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;

最新更新