Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uilongpressgesturerecognizer fire only once

I want to add a gesture that will only fire if a person has been pressing for a second or so. Not a tap but a long press. If I use uilongpressgesturerecognizer it keeps firing until I release my finger. How can I get around this.

like image 928
TheHellOTrofasdasd Avatar asked Jun 14 '16 13:06

TheHellOTrofasdasd


2 Answers

Set minimumPressDuration when you create and add gesture as below:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                                      initWithTarget:self action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;

Write your code in UIGestureRecognizerStateEnded state as below:

-(void)handleLongPress:(UILongPressGestureRecognizer *)Gesture{

    if (Gesture.state == UIGestureRecognizerStateEnded) {


       //Do any thing after long press ended,which will be 1.0 second as set above


    }
    else if (Gesture.state == UIGestureRecognizerStateBegan){



    }
}
like image 107
Ronak Chaniyara Avatar answered Sep 28 '22 09:09

Ronak Chaniyara


Swift 5

Declare a UILongPressGestureRecognizer:

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(gestureAction(gesture:)))

Set its .minimumPressDuration to 1.0 or any interval you want.

Set the recognizers .delegate to your ViewController and add it to your view using .addGestureRecognizer().

Use the following function to handle the gesture:

@objc func gestureAction(gesture: UIGestureRecognizer) {
    if let longPress = gesture as? UILongPressGestureRecognizer {
        if longPress.state == UIGestureRecognizer.State.began {

        } else {

        }
    }
}
like image 27
ixany Avatar answered Sep 28 '22 10:09

ixany