UILongPressGestureRecognizer 不起作用 [英] UILongPressGestureRecognizer didn't work

查看:52
本文介绍了UILongPressGestureRecognizer 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I added 4 gesture recoginzer to a UIImageView , the single tap , double tap and pin gestures work fine . However, long press gesture didn't work . Why is that?

        _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 委托,但这仍然不起作用.

Hey guys, I add the shouldRecognizeSimultaneouslyWithGestureRecognizer method and set the long press gesutre delegate , but that still did not work .

推荐答案

你的错误可能在这里

[longPress requireGestureRecognizerToFail:longPress ] ;

为什么 longPress 要求自身失败?删除它.

Why longPress require failed to itself ? delete it.

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

You don't understand requireGestureRecognizerToFail command. It use when a gesture need other failed to fired. If longPress don't fired, then tapGesture fired. In your case pinch failed -> longPress failed -> double tap failed -> single tap Also delete this line: longPress.numberOfTapsRequired = 1 ;

注释掉您的代码并使用此代码:

Comment out your code and use this code:

- (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");
}

这篇关于UILongPressGestureRecognizer 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆