UILongPressGestureRecognizer不起作用

时间:2013-12-24 14:23:13

标签: ios objective-c uigesturerecognizer

我在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;
}

嘿伙计们,我添加了shouldRecognizeSimultaneouslyWithGestureRecognizer方法并设置了长按gesutre委托,但仍然没有用。

3 个答案:

答案 0 :(得分:4)

您的错误可能在这里

[longPress requireGestureRecognizerToFail:longPress ] ;

为什么longPress要求自己失败?删除它。

您不了解requireGestureRecognizerToFail命令。当手势需要其他未能触发时使用。如果longPress没有触发,则tapGesture触发。   在您的情况下,夹点失败 - > longPress失败 - >双击失败 - >单击 同时删除此行: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");
}

答案 1 :(得分:0)

尝试使用此委托方法: -

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

你也错过了这个: - longPress.delegate = self;

答案 2 :(得分:0)

为了使长按手势识别器工作,单击手势识别器必须首先失败。

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

[longPress requireGestureRecognizerToFail:longPress ] ;

为:

[longPress requireGestureRecognizerToFail:singleTap ] ;