iPhone的iOS如何添加一个UILongPressGestureRecognizer和UITapGestureRecognizer到相同的控制和防止冲突?

我正在构build一个iPhone应用程序,让用户重新排列屏幕上的一些UI元素。

如何添加一个轻拍手势识别器和一个长按手势识别器到相同的UIView? 当我从长按抬起手指时,轻拍手势识别器会闪光。 如何暂时禁用水龙头手势识别器,或防止当用户长时间按下时触发手势识别器?

谢谢!

要允许两个手势一起工作,请实施以下代理方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } 

为了使长按优先,做:

[tapGesture requireGestureRecognizerToFail:longPress];

要成功地结合,你需要:

1º添加到标题处的界面手势委托

 @interface ViewController : ViewController <UIGestureRecognizerDelegate> 

2º创build手势事件并添加到源文件的视图:

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch:)]; [tap setNumberOfTapsRequired:1]; // Set your own number here [tap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTouch:)]; [longTap setNumberOfTapsRequired:0]; // Set your own number here [longTap setMinimumPressDuration:1.0]; [longTap setDelegate:self]; // Add the <UIGestureRecognizerDelegate> protocol [tap requireGestureRecognizerToFail:longTap]; // Priority long [self.view addGestureRecognizer:tap]; [self.view addGestureRecognizer:longTap]; 

3º在源文件中添加callback:

 - (void) touch: (UITapGestureRecognizer *)recognizer { CGPoint location = [recognizer locationInView: self.HUDview]; if (recognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"touch UIGestureRecognizerStateBegan"); } if (recognizer.state == UIGestureRecognizerStateEnded) { NSLog(@"touch UIGestureRecognizerStateEnded"); //NSLog(@"Position of touch: %.3f, %.3f", location.x, location.y); // Position landscape } } - (void) longTouch: (UILongPressGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateBegan) { NSLog(@"longTouch UIGestureRecognizerStateBegan"); } if (recognizer.state == UIGestureRecognizerStateEnded) { NSLog(@"longTouch UIGestureRecognizerStateEnded"); } } 

4º设置手势识别器可用:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 

作为替代方法,不要有两个单独的识别器 – 只要使用LongPress识别器来处理这两个事件:

configuration如下:

 UILongPressGestureRecognizer* longPress = [ [ UILongPressGestureRecognizer alloc ] initWithTarget:self.nextResponder action:@selector(longPressEvent:)]; categoryPanelDrag.minimumPressDuration = 0.0; 

然后处理如下:

 - (BOOL)longPressEvent:(UILongPressGestureRecognizer *)gesture { // _dragStarted is a class-level BOOL if(UIGestureRecognizerStateBegan == gesture.state) { _dragStarted = NO; } if(UIGestureRecognizerStateChanged == gesture.state) { _dragStarted = YES; // Do dragging stuff here } if(UIGestureRecognizerStateEnded == gesture.state) { if (_dragStarted == NO) { // Do tap stuff here } else { // Do drag ended stuff here } } return YES; } 

我确实尝试了手机和熟练人员的方法,但不知何故,他们不适合我的项目,所以我解决了如下,

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ NSLog(@"%@ %ld",touch.description, touch.phase); [self performSelector:@selector(checkTouch:) withObject:touch afterDelay:0.5]; return YES; } 

 - (void)checkTouch:(UITouch *)touch{ NSLog(@"touch phase = %ld",touch.phase); if (touch.phase == UITouchPhaseStationary) { //still holding my hand and this means I wanted longPressTouch } if (touch.phase == UITouchPhaseEnded){ //I released my finger so it's obviously tap } } 

这可能是更简单的解决scheme,但当然这取决于项目。

你可以在代码中处理它,在长按期间,设置一个标志,如果水龙头被调用,而标志是真的或什么的,那么不要执行水龙头代码并重置标志。 我不知道更好的方法