Question

I am using a UILongPressGestureRecognizer. I am wondering if anyone has ever figured out a way to trigger when the minimumPressDuration has been reached without having to lift their fingers. Basically, can we trigger the end of the gesture without having to remove our fingers? Can we just use how much time had passed?

Thanks,

Collin

Was it helpful?

Solution

That's what the UIGestureStateBegan is for:

-(void)handleGesture:(UILongPressGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateBegan){
        NSLog(@"minimum duration elapsed");
    }else if(sender.state == UIGestureRecognizerStateEnded){
        NSLog(@"user lifted their finger");
    }
}

OTHER TIPS

.began state ends when user lifts the finger, so the accepted answer is not correct for this problem. After some time I figured it out. Answer in Swift:

var gesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))

func handleLongPress(){
         //do something
         gesture.addTarget(self, action: #selector(longPress(_:)))
}

@objc func longPress(_ sender: UILongPressGestureRecognizer) {
         if sender.state == .began {
                handleLongPress()
                gesture.removeTarget(self, action: #selector(longPress(_:)))
      }
}

This will cause the gesture to stop tracking touch after .began state is received and re-enable it after you finish doing whatever you want to do after long press

This is very old question but I hope someone will find it useful

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top