uilongpressgesturerecognizer fire only once
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){
}
}
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 {
}
}
}