Creating A Tap or Hold Counter in Swift

Project 2: Tap Or Hold Counter

TapOrHoldCounterVideo

The second project was a way to test my knowledge of the previous project, Tap Counter, and was modified to incorporate the hold gesture.

If we were to build off of the first project, Tap Counter, only a few lines of code would be required to achieve the Tap or Hold Counter.

  1. In the Main.storyboard, add a Long Press Gesture Recognizer and place it on the ‘tap’ button (At this point, you can add the words “or Hold” to the tap button)
  2. Connect the Long Press Gesture Recognizer to the tapButton func
  3. Add a longPressAction function to the class
    • func longPressAction() {
                print("did long press")
          }
  4. Add the following lines of code to implement the UILongPressGestureRecognizer and set the minimumPressDuration time
    • let lpg = UILongPressGestureRecognizer(target: self, action: "longPressAction")
      
      lpg.minimumPressDuration = 2
    • // at the end of the func, before the closing curly brace include the following
      tapOrHold.addGestureRecognizer(lpg)

You should be able to run both apps and successfully increment with each tapButton and reset with the resetButton.

Leave a comment