YosiFZ
YosiFZ

Reputation: 7900

UILongPressGestureRecognizer recognize only when moving my finger

i try to use UILongPressGestureRecognizer in my App and the problem is that this function call only when i move my finger a little bit.

this is the code i am using :

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doRewind)];
    [uiNextButton addGestureRecognizer:longPress];

Upvotes: 2

Views: 1838

Answers (3)

Bushra Shahid
Bushra Shahid

Reputation: 3579

When adding the UILongPressGestureRecognizer, you also need to set the interval for which you want the user to hold. You can do this with the following line of code:

[longPress setMinimumPressDuration:2];

Upvotes: 0

Sabby
Sabby

Reputation: 2592

I know I am late to answer this question, but I think this might help someone. I was having the same problem. I needed to fire the event and move to the next screen without moving or touching up my screen. As the gesture recognizer has different states:

UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded

I was using UIGestureRecognizerStateEnded and that was creating the problem, because it first checks if the state has begun and the event was not firing without moving my finger. So I replaced my UIGestureRecognizerStateEnded state with UIGestureRecognizerStateBegan and everything worked fine. Now you don't need to move your finger away. Just touch hold and everything work fine.

if (gesture.state == UIGestureRecognizerStateBegan) {
     // Do your stuff
}

Thats the correct way, other ways like numberOfTapsRequired, allowableMovement are meant for different purposes.

Upvotes: 3

Perception
Perception

Reputation: 80603

Your UILongPressGestureRecognizer has been created with the bare minimum of configuration information. At a minimum you should look into setting these properties:

  • minimumPressDuration
  • allowableMovement

And in special cases you can also set:

  • numberOfTouchesRequired
  • numberOfTapsRequired

In your case I think you want to set the allowableMovement to 0, the default value is 10 (pixels). You can read more from the class reference I linked.

Upvotes: 2

Related Questions