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...

有帮助吗?

解决方案 3

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.

其他提示

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!

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top