1

Here is my code first:

@IBAction func SOSLongPressed(_ sender: UILongPressGestureRecognizer) {
    //sender.numberOfTapsRequired = 2
    sender.minimumPressDuration = 3

    if sender.state == .began {
        print("long pressed")
    }
}

Problems:

  1. "long pressed" is printed at around 0.5 (rather than 3) second after I started to press the view only for the first time after my app launched. In other words, after the app is launched, the UILongPressGestureRecognizer works properly for the 2nd or the 3rd (and so on) time, i.e., print "long pressed" after 3 seconds. But not for the first time.

  2. If I uncomment the sender.numberOfTapsRequired = 2 line, "long pressed" is also printed at around 0.5 second even with only 1 tap and hold. After that, even if I double tap and hold for 3 seconds, "long pressed" won't be printed out anymore.

No idea why is that and hope someone can help me out.

Thanks in advance!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
D.D
  • 11
  • 2

1 Answers1

2

You are updating the values of the recognizer only after it's been recognized the first time.

You should set the values for those properties right after you create the recognizer. If it was created in Interface Builder, you should be able to set the values there as well.

Edit to answer comment:

Try increasing the tolerance (allowableMovement) to like 30 or 50. Your finger might be moving enough within those 3 seconds for the recognizer to fail.

I can't comment on the numberOfTaps property since I've never used it.

PS. You can modify all these settings from Interface Builder or programmatically, it won't make a difference. What will make a difference is that you set the properties at the correct time, not after the gesture has been recognized.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • Thanks so much for your response. But even if I recreated the recognizer programmatically and deleted the one built in Interface Builder, I still got some weird issues: 1. if `minimumPressDuration` is set to any value >= 3, the recognizer won't work; 2. if `numberOfTapsRequired` is set to 1, it actually requires 2 taps. Is this a bug or swift simply works this way? – D.D Oct 23 '18 at 02:34