7

guys,

I have added two UILongPressGestureRecognizer. What I want is when two buttons are pressed down for 0.3 second, fire up "shortPressHandler". If user keeps pressing these two buttons for another 1.2 second, fire up "longPressHandler". Now I only get shortPressHandler fired up and the longPressHandler was never fired. I think it might because the shortPressGesture is recognized first and longPressGesture never gets a chance. Can anyone show me how to achieve what I want? Thanks in advance.

UILongPressGestureRecognizer *longPressGesture =[[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)] autorelease];
longPressGesture.numberOfTouchesRequired = 2;
longPressGesture.minimumPressDuration = 1.5;
longPressGesture.allowableMovement = 10;
longPressGesture.cancelsTouchesInView = NO;
longPressGesture.enabled = true;
[self.view addGestureRecognizer:longPressGesture];

UILongPressGestureRecognizer *shortPressGesture =[[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(shortPressHandler:)] autorelease];
shortPressGesture.numberOfTouchesRequired = 2;
shortPressGesture.minimumPressDuration = 0.3;
shortPressGesture.allowableMovement = 10;
shortPressGesture.cancelsTouchesInView = NO;
shortPressGesture.enabled = true;
[self.view addGestureRecognizer:shortPressGesture];
Globalhawk
  • 148
  • 2
  • 8
  • Thanks. Do you know what to do if I also want shortGesture to fire even if the buttons are not released? – Globalhawk Mar 15 '12 at 12:42
  • Sorry, i didn't see this message - if you write it under your post you have to address it with '@name...' if i understand you right you want something like: user presses, shortPress gets fired after 0.3s and if he doesn't release his press longPress gets fired after 1.2s. Is that correct? – Rok Jarc Mar 15 '12 at 15:37
  • @rokjarc That's exactlly what I want. Thanks. – Globalhawk Mar 15 '12 at 17:18
  • this one is a bit trickier: one quick and dirty way is to call shortPressHandler from longPressHandler. If you need shortPressHandler to be called immidiately after 0.3s press then you will most probably have to sublass `UIGestureRecognizer`. – Rok Jarc Mar 15 '12 at 17:24
  • @rokjarc Thanks a lot. Can you please give a little help on which function I shall override? – Globalhawk Mar 15 '12 at 18:51
  • Here you go, see updated answer. And welcome to Stack Overflow! :) – Rok Jarc Mar 15 '12 at 19:49

1 Answers1

7

Insert this line before adding shortPressGesture:

 [shortPressGesture requireGestureRecognizerToFail:longPressGesture];

Note: shortGesture will not get called immediately after 0.3 seconds of holding but when the tap is released if it was between 0.3 seconds and 1.2 seconds long. If the tap is longer than 1.2 s (you have 1.5s in your code which is probably a typo) only longPressGesture will fire up.

EDIT:

However, if you want your event handlers both to fire (in an event of long press), you should do this:

Your UIView of should implement the <UIGestureRecognizerDelegate> in .h file.

In .m file you add this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}

Now instead of adding the line:

[shortPressGesture requireGestureRecognizerToFail:longPressGesture];

You add this two lines:

shortPressGesture.delegate = self;
longPressGesture.delegate = self;

NOTE: if you have any other UIGestureRecognisers linked to your UIVIew you will have to add some checking in shouldRecognizeSimultaneouslyWithGestureRecognizer:, otherwise you can simply return YES.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124