UIGestureRecognizer 的方法-(NSUInteger)numberOfTouches 可以告诉您在您的视图上放置了多少触摸。这个Event Handling Guide也可以帮助你:)
另一种方法是UITapGestureRecognizer,可以使用numberOfTouchesRequired 进行配置,以将一个识别器限制为特定数量的手指。
编辑
如果另一个手势识别器处于活动状态,我建议您使用私有 BOOL 来锁定与其中一个手势识别器的交互。
借助 XCode 4 及更高版本中可用的新 LLVM 编译器,您可以在实现 (.m) 文件中的默认类别中声明 @private 变量:
@interface YourClassName() {
@private:
BOOL interactionLockedByPanRecognizer;
BOOL interactionLockedByGestureRecognizer;
}
@end
@implementation YourClassName
... your code ...
@end
你处理平移交互的方法(我假设你会在最后做一些动画来移动东西):
- (void)handlePan:(id)sender
{
if (interactionLockedByGestureRecognizer) return;
interactionLockedByPanRecognizer = YES;
... your code ...
[UIView animateWithDuration:0.35 delay:0.0 options:UIViewAnimationCurveEaseOut
animations:^{
[[sender view] setCenter:CGPointMake(finalX, finalY)];
}
completion:^( BOOL finished ) {
interactionLockedByPanRecognizer = NO;
}
];
}
现在您只需检查您的touchesBegan、touchesMoved 和touchesEnded 内部是否交互被 UIPanGestureRecognizer 锁定:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (interactionLockedByPanRecognizer) return;
interactionLockedByGestureRecognizer = YES;
... your code ...
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (interactionLockedByPanRecognizer) return;
... your code ...
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (interactionLockedByPanRecognizer) return;
... your code ...
interactionLockedByGestureRecognizer = NO;
}