Search code examples
swift3watchkitapple-watchwatchos-3

How to simulate a whilePressing TouchEvent in WatchKit 3?


What I want:

I want to have a progress bar filling up to 100% while pressing a button. E.g.: Button is pressed and progress bar keeps filling continuously only while being pressed until 2 seconds have passed and progress bar hits 100% after the 2 seconds.

What I've done:

I tried to simulate a whilePressing TouchEvent using Long Press Gesture Recognizers and Tap Gesture Recognizer, but didn't get a satisfying result. Main problem is, that I can't mix a Tap Gesture with a Long Press Gesture on the same button. If I could do this, I could at least get the touchDown (Tap) and touchUp (Long Press) Events and try simulate my wanted behavior with that.

Does anyone have an advise or is it simply not possible with the currently given framework?


Solution

  • Figured out a satisfying workaround. I use a WKLongPresssGestureRecognizer with my button that is supposed to have the desired interaction. I've set the Min Duration in the Attributes Inspector to 0. And used the following snippet as an IBAction for my Recognizer:

    @IBAction func pressAndHold(_ sender: AnyObject) {
        guard let sender = sender as? WKLongPressGestureRecognizer else {
            return
        }
        if sender.state == WKGestureRecognizerState.began {
            print("Touch down")
        } else if sender.state == WKGestureRecognizerState.ended {
            print("Touch up")
        }
    }