How to code a screen tap to increment/decrement a value?

I would like to have a label that when tapped once will increment by 1 (+1) or when double tapped will decrement by 1 (-1).

Suggestions? Ideas?

Thanks
Implement tapGesture on the label:
  • one with numberOfTapsRequired set to 1

  • another with numberOfTapsRequired set to 2

You can add them via the storyboard by dragging tap gestures on the label.

Connect each gesture to an IBAction, by control-drag from the IBAction to the corresponding gesture.
Code Block
@IBAction func handleLabelTap(recognizer:UITapGestureRecognizer) {
print("Label Tapped")
// Decrement the value
value = label.text ?? 0
value += 1
label.text = String(value)
var value = Int(label.text ?? "0") ?? 0
value -= 2 // Because singletap will be fired first, need to compensate for the +1
label.text = String(value)
}
@IBAction func handleLabelDoubleTap(recognizer:UITapGestureRecognizer) {
print("Label Double Tapped")
// Increment the value
var value = Int(label.text ?? "0") ?? 0
value += 1
label.text = String(value)
}

Very important: enable user interaction for the label


How to code a screen tap to increment/decrement a value?
 
 
Q