2

As the title reads, my problem is that my UILongPressGestureRecognizer sometimes does not run the code inside the sender.state = .ended. The .began always runs and works. I have tried to notice pattern but is to infrequent and I have not found a valid pattern or causation. I simply add my UITapGestureRecognizer UILongPressGestureRecognizer to my button:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
tapGesture.numberOfTapsRequired = 1
camButton.addGestureRecognizer(tapGesture)

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
longGesture.minimumPressDuration = 0.10
camButton.addGestureRecognizer(longGesture)

And then here is my longTap function:

    @objc func longTap(_ sender: UIGestureRecognizer) {
            if sender.state == .ended {
                if movieOutput.recordedDuration.seconds == lastRecordDuration || movieOutput.recordedDuration.seconds <= 0.35 {
                    capturePhoto()
                    } else {
                    stopRecording()
                }
           } else if sender.state == .began {
            startCapture()
        }
    }

I use the longPress for video and photos, and the TapGesture for photos only. Im using AVFoundation.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What about the `canceled` state? What if you don't add the tap gesture? Does that change anything for the long press? – rmaddy Jan 11 '19 at 00:08
  • no, im pretty 96.5% confident that it does not change the long gesture recognizer. I intentionally set the longGesture minimum hold to 0.10 so that it overlaps less with the normalTapGesture. However if you are thinking that it is recognizing tapGesture and not the longGesture, just know that the .began _does_ in fact run it just never .ended, and this has happened when recording for a while. –  Jan 11 '19 at 00:21
  • Instead of capturing, you may try a simple output command, like "print( "end")". Maybe that can separate the issue from complex – E.Coms Jan 11 '19 at 00:41
  • @Guilermoramirezblonski But what about the `canceled` state (or any other states)? – rmaddy Jan 11 '19 at 00:42
  • @rmaddy I dont currently have those active in my code, however I have not looked into those sender states. Should I implement them? So that if it cancels, it should have same code as .ended? –  Jan 11 '19 at 00:57
  • You are telling us that the `ended` state is never reached. So I'm asking you to check to see if the `canceled` state is being called instead. – rmaddy Jan 11 '19 at 01:12
  • Oh ok, sorry, I did not understand :\. –  Jan 11 '19 at 01:47
  • @rmaddy ok yes it did call `sender.state == .cancelled`, so I imagine I should put the .ended code inside the .cancelled as well?... –  Jan 11 '19 at 01:51
  • It might be worth trying to figure out why the long press is being cancelled and not ended. But depending on your needs, change `if sender.state == .ended {` to `if sender.state == .ended || sender.state == .canceled {` – rmaddy Jan 11 '19 at 03:31
  • @rmaddy oh yeah, that is a much more simple way of doing it. Ok, yes I will investigate this more, but thank you for pointing me in the right direction on this problem. –  Jan 11 '19 at 03:56

2 Answers2

1

After receiving assistance from @rmaddy, the solution is basically to implement a .cancelled state action. For some reason the continuous gesture of UILongPressGesture got cancelled. In my code I implemented a `if sender.state == .cancelled.

0

I know this is old question, but writing for those who are facing issue like me.

You can get sender.state == .possible when tap is ended.

Niraj
  • 207
  • 1
  • 3
  • 11