Ios UILongPressGestureRecognitor没有';行不通

Ios UILongPressGestureRecognitor没有';行不通,ios,objective-c,uigesturerecognizer,Ios,Objective C,Uigesturerecognizer,我在UIImageView中添加了4个手势识别器,单点击、双点击和锁定手势都可以正常工作。然而,长按手势并没有起作用。为什么呢 _imageView.userInteractionEnabled = YES ; //single tap UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(singleTap

我在UIImageView中添加了4个手势识别器,单点击、双点击和锁定手势都可以正常工作。然而,长按手势并没有起作用。为什么呢

        _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;
}

嘿,伙计们,我添加了
shouldRecognitize同时使用GestureRecognitizer
方法,并设置了长按gesutre委托,但仍然不起作用。

尝试使用此委托方法:-

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
 }
您还遗漏了以下内容:-
longPress.delegate=self

您的错误可能在这里

[longPress requireGestureRecognizerToFail:longPress ] ;
为什么longPress要求自己失败?删除它

您不了解requiregestureerecognizertofail命令。当一个手势需要另一个没有被触发时使用。如果longPress没有发射,那么Tappress发射。 在您的情况下,挤压失败->长按失败->双点击失败->单点击 同时删除该行: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");
}

要使长按手势识别器工作,必须首先使单点手势识别器出现故障

在代码中,您指定长按手势识别器本身必须失败(这是错误的)。您应该更改:

[longPress requireGestureRecognizerToFail:longPress ] ;
致:


添加:-longPress.delegate=self;相关:对不起,我的原始代码是
[longPress requiregestureerecognizertofail:singleTap],我删除了这行代码,长按仍然无法工作。这是错误的!使用[singleTap RequiremeTestureRecognitizerToFail:longPress];
[longPress requireGestureRecognizerToFail:singleTap ] ;