0

I created two Long Press Gesture Recognizers in IB, and created two IBActions for them.

@IBAction func longPressGesture(_ gesture: UILongPressGestureRecognizer) {
    print("Do long press")

}


@IBAction func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) {
    print("Do something else")

}

longPressGesture is set to > 0.5, 0 taps, 1 touch.

longPressTapGesture is set to > 0.5, 1 tap, 1 touch.

So technically, when I start the app and just press down anywhere on the screen, I should get longPressGesture to fire.

Instead, the very first time after I started running the app, it's always the longPressTapGesture firing, no matter what gestures I use.

If I lift finger, then press down again, longPressGesture fires this time.

Any suggestions why longPressTapGesture is firing even though I'm just doing a single long press?

Thanks.

Bevan
  • 179
  • 1
  • 12
  • What state is the gesture recognizer in when the function is called? – D. Mika Feb 25 '18 at 15:22
  • UILongPressGestureRecognizer has two states, began and ended. Try this: `@IBAction func longTap(_ sender: UIGestureRecognizer){ print("Long tap") if sender.state == .ended { print("UIGestureRecognizerStateEnded") //Do Whatever You want on End of Gesture } else if sender.state == .began { print("UIGestureRecognizerStateBegan.") //Do Whatever You want on Began of Gesture } }` – Anup G Prasad Feb 25 '18 at 15:38
  • It didn’t matter what state it was in. The wrong function was called. – Bevan Feb 26 '18 at 00:34

1 Answers1

1

@Bevan, based on the testing I've done, you're not doing anything wrong. It appears as though something goes wrong when adding gesture recognizers through the storyboard. When I build a test application using storyboard and set the settings you've described above I see the exact issue you are seeing. However, when I create the same setup in code the behavior functions as you would expect. Maybe the best bet here would be to use code rather than storyboards for this scenario.

Code sample below:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let tap = UILongPressGestureRecognizer(target: self, action: #selector(longPressTapGesture))
        tap.minimumPressDuration = 0.5
        tap.numberOfTapsRequired = 1
        tap.numberOfTouchesRequired = 1
        view.addGestureRecognizer(tap)
        let press = UILongPressGestureRecognizer(target: self, action: #selector(longPressGesture))
        press.minimumPressDuration = 0.5
        press.numberOfTouchesRequired = 1
        view.addGestureRecognizer(press)
    }

    @objc func longPressGesture(_ gesture: UILongPressGestureRecognizer) {
        print("Do long press")

    }

    @objc func longPressTapGesture(_ gesture: UILongPressGestureRecognizer) {
        print("Do something else")

    }
}
Steve
  • 921
  • 1
  • 7
  • 18
  • Thanks @Steve! I thought I was going crazy! I did have them in code before but I like the cleanness and consistency of the IB where I can just set them up with IBAction like a button so I changed it. I suspect it's a bug but looks like I'll have to go back to using code! Thanks so much for confirming it and with sample code! – Bevan Feb 26 '18 at 00:49
  • No problem glad it helped. – Steve Feb 26 '18 at 02:29
  • do you know how I can implement "drag scroll"? i.e. tap the screen and while holding down, drag up or down and make the content of a text view scroll up or down? – Bevan Feb 26 '18 at 09:38
  • It also works if you implement "half and half" - one using IB, the other using code. Must be a bug in Xcode IB. – Bevan Feb 26 '18 at 10:54
  • I definitely think it’s an IB bug as for the dragging question. Are you asking about scrolling the text in a text view? That would be the default behavior of the textview if the frame of the textview is smaller than the size of the content. – Steve Feb 26 '18 at 13:04
  • I want to do something siimilar to that, like dragging and scrolling email up and down. What happens is I have a small textview area where I want to let users drag and scroll on the rest of the screen while scrolling the small textview area up and down. – Bevan Feb 26 '18 at 13:07
  • I’m not sure I understand. There’s a textview on a scrollview? And you want both to move at the same time? Please try to explain it again or since this is moving towards a separate problem maybe it’s worth adding a new question to SO for it. – Steve Feb 26 '18 at 13:10
  • Basically like swipe gesture but instead of the action happening after I swipe, I want it to happen as I'm swiping (as long as my finger is on the glass). I'll put a new question to SO but I want to make sure I get my terminology right. I'm not sure if what I want to do is considered "pan". Anyway, thanks for your help. Much appreciated. – Bevan Feb 26 '18 at 15:24
  • I'm sorry I'm still a bit confused, when you write the next question I'll take a look, maybe some screenshots of the view you're attempting to make will help people understand the ask. In any case, best of luck with your app. – Steve Feb 26 '18 at 15:26
  • Just did screenshot and posting soon. Thanks. – Bevan Feb 26 '18 at 15:33