我有 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/