0

Giving thanks in advance, I would like to share the strange behaviour of UILongPressGestureRecognizer.

UIView *v = [UIView alloc] initWithFrame:CGRectMake(0,0,20,20)];
UILongPressGestureRecognizer *longpressGesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[v addGestureRecognizer:longpressGesture1];

and here the delegate to handle the gesture recogniser.

-(IBAction)tapped:(UILongPressGestureRecognizer *) gesture

{ 
    switch ([gesture state]) {
        case UIGestureRecognizerStateBegan:
        {

            NSLog(@"Long Tap detacted.");  

        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            NSLog(@"UIGestureRecognizerStateChanged");


        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"Long Tap ended.");

        }
            break;

    }

}

This piece of code is working perfectly as expected in iOS 4 and 5, but in ios 6 with retina display when we perform the long tap, UIGestureRecognizerStateBegan is being called twice for a single long tap resulting in a application crash. Any help is greatly welcome.

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55
  • Please see http://stackoverflow.com/a/3320351/792677 Feel free to provide and accept an answer with the fixed code. – A-Live Mar 12 '13 at 14:09

1 Answers1

2

UILongPressGestureRecognizer is a continuous event recognizer. You have to look at the state to see if this is the start, middle or end of the event and act accordingly.

Its calling two times because you are pressing and removing your finger.

First call is indicating you that there is an Long tap detected i.e. UIGestureRecognizerStateBegan Second call is indicating you that there is end of that tap i.e. UIGestureRecognizerStateBegan

There are three state of tap

  1. UIGestureRecognizerStateBegan
  2. UIGestureRecognizerStateChanged
  3. UIGestureRecognizerStateEnded

If you will drag your finger then it will called multiple time that will indicate that there is some changes in its state.

Follow UILongPressGestureRecognizer Class Reference for more

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74