0

I have a UIView that has a UILongPressGestureRecognizer in it like this:

    movementGestureRecognizer = [[UILongPressGestureRecognizer alloc] init];
    [movementGestureRecognizer setDelegate:self];
    [movementGestureRecognizer setMinimumPressDuration:0.0f];
    [self addGestureRecognizer:movementGestureRecognizer];

Has you can see by the name of it, it's used to, as soon, as I long press the UIView, I am able to move it around.

The thing is, I want also to be able to add some other kind of gestures, for example:

       optionsGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(options:)];
        [optionsGestureRecognizer setNumberOfTapsRequired:2];
        [self addGestureRecognizer:optionsGestureRecognizer];

The problem is that, I am not able to call options: because the movementGestureRecognizer is "sucking" all the gestures. Is there are a way to prevent, or cancel the movementGestureRecognizer or delay it?


Edit 1.0

I am able to call options: from the TapGestureRecognizer if I do the following:

    [movementGestureRecognizer setMinimumPressDuration:0.1f];

Still, it's not the perfect solution in terms of usability...

Rui Peres
  • 25,741
  • 9
  • 87
  • 137

3 Answers3

0

I'm not quite sure but can you try to implement the gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: delegate like this maybe:

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

additionally you can specify dependencies between gesture recognizers like this:

[optionsGestureRecognizer requireGestureRecognizerToFail: movementGestureRecognizer];

I'd suggest that you read up the details in the UIGestureRecognizer Class Reference!

You can find also a good post elaborating on the very same topic here!

Community
  • 1
  • 1
Nenad M
  • 3,055
  • 20
  • 26
0

The difference between a tap and a long press is in how long you hold your finger down before releasing it from the touch surface. Therefore, you would want to have a minimum press duration of greater than 0.

In addition, you can do the following:

movementGestureRecognizer.delaysTouchesBegan = NO;
movementGestureRecognizer.delaysTouchesEnded = NO;

This would allow the system to recognize taps as well as long presses.

Hitesh
  • 1,413
  • 1
  • 11
  • 11
0

I was able to come with a solution by doing the following:

1) Implementing a "state machine" on my UIView, by disabling and enabling UIGestureRecognizers based on the state of it.

2) Use a UIPanGestureRecognizer for moving the UIView's around.

3) And this question.

Community
  • 1
  • 1
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • Interesting! I'm also looking for a "finite state machine" solution for managing the state of a rather complex menu. If i may ask, did you write your "state machine" from scratch or did you consider using something like the [State Machine Compiler](http://smc.sourceforge.net/)? Or would the State Machine Compiler be overkill for your purpose? – Nenad M Oct 17 '12 at 14:13
  • There are 3 states on my `UIView`: normal state, the add new sub `UIView` state, resize state. Based on this 3 states, I enable and disable the `UIGesturesRecognizers`, so they don't overlap each other. At the moment I have 6 `UIGesturesRecognizers` and everything works fine. I also did some stuff with the UIScrollGesture so it doesn't get in "the way" while you are doing your stuff with the other Gestures. – Rui Peres Oct 17 '12 at 14:27