ios - UILongPressGestureRecognizer 不起作用

标签 ios objective-c uigesturerecognizer

我在 UIImageView 中添加了 4 个手势识别器,单击、双击和 pin 手势工作正常。但是,长按手势不起作用。这是为什么?

        _imageView.userInteractionEnabled = YES ;

    //single tap
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(singleTapAction:) ]  ;
    singleTap.numberOfTapsRequired = 1 ;
    singleTap.numberOfTouchesRequired = 1 ;

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)] ;
    doubleTap.numberOfTouchesRequired = 1 ;
    doubleTap.numberOfTapsRequired = 2 ;
    [singleTap requireGestureRecognizerToFail:doubleTap] ;

    //pin gesture
    UIPinchGestureRecognizer  *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)] ;

    //long press gesture
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:) ] ;
    [longPress requireGestureRecognizerToFail:singleTap ] ;
    longPress.minimumPressDuration = 1 ;
    longPress.numberOfTouchesRequired = 1 ;
    longPress.numberOfTapsRequired = 1 ;

    [_imageView addGestureRecognizer:longPress] ;
    [_imageView addGestureRecognizer:pin] ;
    [_imageView addGestureRecognizer:singleTap] ;
    [_imageView addGestureRecognizer:doubleTap] ;


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

大家好,我加了shouldRecognizeSimultaneouslyWithGestureRecognizer方法并设置长按 gesutre 委托(delegate),但这仍然不起作用。

最佳答案

你的错误可能在这里

[longPress requireGestureRecognizerToFail:longPress ] ;

为什么 longPress 要求本身失败?删除它。

您不了解 requireGestureRecognizerToFail 命令。它在手势需要其他未能触发时使用。如果 longPress 未触发,则触发 tapGesture。
在你的情况下捏失败->长按失败->双击失败->单击
同时删除这一行: longPress.numberOfTapsRequired = 1 ;

注释掉您的代码并使用此代码:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.imageView.userInteractionEnabled = YES;
    self.imageView.multipleTouchEnabled = YES;
    UIGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 1 ;
    longPress.numberOfTouchesRequired = 1 ;
    [longPress requireGestureRecognizerToFail:pinchGesture];

    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    doubleTap.numberOfTouchesRequired = 1;
    doubleTap.numberOfTapsRequired = 2;
    [doubleTap requireGestureRecognizerToFail:longPress];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    singleTap.numberOfTouchesRequired = 1;
    singleTap.numberOfTapsRequired = 1;
    [singleTap requireGestureRecognizerToFail:doubleTap];

    [self.imageView addGestureRecognizer:pinchGesture];
    [self.imageView addGestureRecognizer:longPress];
    [self.imageView addGestureRecognizer:doubleTap];
    [self.imageView addGestureRecognizer:singleTap];

}

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gesture{
    NSLog(@"Pinch");
}


- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture{
    NSLog(@"LongPress");
}

- (void)handleDoubleTap:(UITapGestureRecognizer *)gesture{
    NSLog(@"Double Tap");
}

- (void)handleSingleTap:(UITapGestureRecognizer *)gesture{
    NSLog(@"Single Tap");
}

关于ios - UILongPressGestureRecognizer 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20762513/

相关文章:

ios - 从 Craigslist 下载图像

ios - objective-C :旋转方法不起作用

ios - 当手指在该 View 内的 UIButton 上时,触摸时拖动 UIView 不起作用

ios - 将新的 UIImage 添加到 SubView 需要很长时间

android - 来自/root/.pub-cache/hosted/pub.dartlang.org 包的 Flutter 调试编译错误

ios - 滚动回到顶部时显示导航栏。操作系统

ios - 如何检测滑动方向、触摸位置等?

objective-c - 禁用手势识别器

ios - 使用 Xcode7 的 UI 测试为 App Store 创建应用程序屏幕截图

ios - 在群组和公共(public)聊天的情况下,如何使用 QMServicesManager 从对话框列表中删除对话框?