开发者

UILongPressGestureRecognizer recognize only when moving my finger

开发者 https://www.devze.com 2023-03-25 12:36 出处:网络
i try to use UILongPressGestureRecognizer in my App开发者_如何学C and the problem is that this function call only when i move my finger a little bit.

i try to use UILongPressGestureRecognizer in my App开发者_如何学C 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];


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.


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.


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消