21

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

如何将点击手势识别器和长按手势识别器添加到同一个 UIView?当我从长按中抬起手指时,点击手势识别器会触发。如何在用户执行长按时暂时禁用轻击手势识别器或阻止它触发?

谢谢!

4

5 回答 5

56

要让两种手势协同工作,请实现以下委托方法:

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

为了使长按具有第一优先权,请执行以下操作:

[tapGesture requireGestureRecognizerToFail:longPress];

于 2012-03-15T21:55:13.427 回答
11

要成功结合两者,您需要:

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

@interface ViewController : ViewController <UIGestureRecognizerDelegate>

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

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º 在源文件中添加回调:

- (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;
}
于 2015-09-16T10:59:14.563 回答
8

作为一种替代方法,不要有两个单独的识别器 - 只需将 LongPress 识别器用于两个事件:

配置如下:

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;

}
于 2013-01-24T14:37:38.853 回答
2

我确实尝试了 moby 和熟练工的方法,但不知何故它们不适合我的项目,所以我解决了如下问题,

-(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 
    }
}

它可能是更简单的解决方案,但当然它取决于项目。

于 2015-08-30T07:34:24.273 回答
0

您可以在代码中处理它,在长按期间设置一个标志,如果在标志为真或其他任何情况下调用点击,则不要执行点击代码并重置标志。我不知道更好的方法

于 2012-03-15T21:44:55.020 回答