2

I have Multiple Buttons on my UI, and I want to execute different functions according to different types of click,

  • Single Click
  • Double Click
  • Long Press

Doing it for single tap was easy for me, an IBAction with all the four buttons connected to it, but for the other types of clicks i was stuck,

I understand that i need to use the tap gesture recognizer, but I'm unable to set it to multiple UIButtons,

Here is and example of what I want to do

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var Label: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func ButtonSingleTap(sender:UIButton!) {

    let ButtonNumber: String = String(sender.tag)
    Label.text = "Button " + ButtonNumber + " is Single Tapped"
}


func ButtonDoubleTap(sender:UIButton!) {

    let ButtonNumber: String = String(sender.tag)
    Label.text = "Button " + ButtonNumber + " is Double Tapped"
}


func ButtonLongTap(sender:UIButton!) {

    let ButtonNumber: String = String(sender.tag)
    Label.text = "Button " + ButtonNumber + " is Long Pressed"
}
}

The UI Interface

IKavanagh
  • 6,089
  • 11
  • 42
  • 47

1 Answers1

6

You cannot use one instance of UIGestureRecognizer for several buttons. Each button needs its own set of gesture recognizers.

Here is an example to show you how to do it:

for button in [button1, button2, button3, button4] {
    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("didLongPress:"))
    let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: Selector("didDoubleTap:"))
    doubleTapRecognizer.numberOfTapsRequired = 2
    button.addGestureRecognizer(longPressRecognizer)
    button.addGestureRecognizer(doubleTapRecognizer)
}

func didLongPress(recognizer: UILongPressGestureRecognizer) {
    guard let button = recognizer.view as? UIButton else { return }
    switch recognizer.state {
    case .Began:
        print("long press began button \(button.tag)")
    case .Ended:
        print("long press ended button \(button.tag)")
    default:
        break
    }
}

func didDoubleTap(recognizer: UITapGestureRecognizer) {
    guard let button = recognizer.view as? UIButton else { return }
    print("double tap button \(button.tag)")
}

For this to work you have to add Outlets for your buttons (button1, button2, button3, button4).

joern
  • 27,354
  • 7
  • 90
  • 105
  • Adding the Gestures Recognizer was okay, how can i then pass the button tag to the function i called ? – Sevan Malatjalian Oct 29 '15 at 06:18
  • It worked perfectly, thank you ... but can you briefly tell me what does the guard let statement does ? Appreciate your help – Sevan Malatjalian Oct 29 '15 at 08:22
  • The guard statement makes sure that the object that the user tapped / long pressed on is of type `UIButton`. In your case it is not really needed, but if you wanted to access a property that is only defined in `UIButton` (like `buttonType`) you would have to make sure that the recognizer's `view` property is indeed a `UIButton` or you'd get a compiler error. – joern Oct 29 '15 at 09:39
  • Thanks! This should be the accepted answer. It reminded me to put the `let` `UILongPressGestureRecognizer` declaration inside the `for` statement. Thanks! – user4806509 Jul 27 '16 at 07:53
  • Thanks! I added the gestures to many uibuttons with viewWithTag(i) using array of int, instead outlets because long arrays of outlets makes Xcode go into indexing forever. – theMouse Mar 31 '17 at 23:49