简体   繁体   English

iPhone iOS如何将UILongPressGestureRecognizer和UITapGestureRecognizer添加到同一控件并防止冲突?

[英]iPhone iOS how to add a UILongPressGestureRecognizer and UITapGestureRecognizer to the same control and prevent conflict?

I'm building an iPhone app that would let the user rearrange some of the UI elements on the screen. 我正在构建一个iPhone应用程序,让用户重新排列屏幕上的一些UI元素。

How can I add a tap gesture recognizer and a long press gesture recognizer to the same UIView? 如何在同一个UIView中添加点击手势识别器和长按手势识别器? When I lift up the finger from the long press, the tap gesture recognizer fires. 当我从长按中抬起手指时,轻敲手势识别器会触发。 How can I temporarily disable the tap gesture recognizer or prevent it from firing when the user is performing a long press? 如何在用户执行长按时暂时禁用轻击手势识别器或阻止其触发?

Thank you! 谢谢!

To allow both gestures to work together, implement the following delegate method: 要允许两个手势一起工作,请实现以下委托方法:

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

To make it so that the long press has first priority, do: 为了使长按具有第一优先权,请执行以下操作:

[tapGesture requireGestureRecognizerToFail:longPress];

To combine successfully both you need: 要成功结合您需要:

1º Add to interface gesture delegate at header 1º添加到标题处的界面手势委托

@interface ViewController : ViewController <UIGestureRecognizerDelegate>

2º Create gesture events and add to a view into source file: 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º Add callbacks in source file: 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º Set gesture recognizer available: 4º设置手势识别器可用:

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

As an alternative approach, don't have two separate recognisers - just use the LongPress recogniser for both events: 作为替代方法,不要有两个单独的识别器 - 只需对两个事件使用LongPress识别器:

Configure as follows: 配置如下:

UILongPressGestureRecognizer* longPress = [ [ UILongPressGestureRecognizer alloc ] initWithTarget:self.nextResponder action:@selector(longPressEvent:)];
    categoryPanelDrag.minimumPressDuration = 0.0;

Then handle as follows: 然后处理如下:

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

}

I did try moby and journeyman's approach but somehow they didn't fit my project well, so I solved like below, 我确实试过了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;
}

and

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

It could be simpler solution but of course it depends to project. 它可能是更简单的解决方案,但当然这取决于项目。

You could take care of it in the code, that during the long press, set a flag, and if the tap gets called while the flag is true or whatever then don't execute the tap code and reset the flag. 您可以在代码中处理它,在长按期间设置一个标志,如果在标志为真或其他任何时候调用tap,则不执行tap代码并重置标志。 I don't know a better way 我不知道更好的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将UITapGestureRecognizer和UILongPressGestureRecognizer添加到UIButton - Not able to add UITapGestureRecognizer and UILongPressGestureRecognizer to a UIButton UITapGestureRecognizer被识别为UILongPressGestureRecognizer - UITapGestureRecognizer recognized as UILongPressGestureRecognizer 如何将UILongPressGestureRecognizer添加到UITextField? - How to add a UILongPressGestureRecognizer to a UITextField? 如何将UILongPressGestureRecognizer添加到UITextField? - How to add UILongPressGestureRecognizer to a UITextField? 为什么UILongPressGestureRecognizer可以在iOS模拟器中运行但不能在iPhone上运行 - Why UILongPressGestureRecognizer work in iOS simulator but not work for iPhone iPhone iOS UITapGestureRecognizer是否可以用某个接触点初始化识别器? - iPhone iOS UITapGestureRecognizer is it possible to initialize a recognizer with a certain touch point? 如何使iOS应用在iPhone 5,iPhone 6和iPhone 6+中的外观相同 - How make iOS app look same in iPhone 5, iPhone 6 and iPhone 6+ iOS iPhone在标签控件下方添加标签和文本字段 - iOS iPhone Add label and text field below tab control iPhone / iOS自定义控件 - iPhone / iOS custom control iPhone - 如何在ios中添加AdSupport.framework? - iPhone - How to add AdSupport.framework in ios?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM