23

I'm having a little trouble with the gestures.

I'm trying to use both tap and long press on the same button, so I've used

@IBAction func xxx (sender: UITapGestureRecognizer)

and

@IBAction func xxx (sender: UILongPressGestureRecognizer)

but my button seems to react to both functions when I tap. What might be wrong?

func long(longpress: UIGestureRecognizer){
    if(longpress.state == UIGestureRecognizerState.Ended){
    homeScoreBool = !homeScoreBool
    }else if(longpress.state == UIGestureRecognizerState.Began){
        print("began")
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Alvin Wan
  • 233
  • 1
  • 2
  • 5

1 Answers1

65

Hard to say what´s not working with your code, with the only two rows that you have provided, but I would recommend you to do it in this way instead:

Create an outlet to your button instead

@IBOutlet weak var myBtn: UIButton!

And in your viewDidLoad() add the gestures to the buttons

let tapGesture = UITapGestureRecognizer(target: self, action: "normalTap")  
let longGesture = UILongPressGestureRecognizer(target: self, action: "longTap:")
tapGesture.numberOfTapsRequired = 1
myBtn.addGestureRecognizer(tapGesture)
myBtn.addGestureRecognizer(longGesture)

And then create the actions to handle the taps

func normalTap(){

    print("Normal tap")
}

func longTap(sender : UIGestureRecognizer){
    print("Long tap")
    if sender.state == .Ended {
    print("UIGestureRecognizerStateEnded")
    //Do Whatever You want on End of Gesture
    }
    else if sender.state == .Began {
        print("UIGestureRecognizerStateBegan.")
        //Do Whatever You want on Began of Gesture
    }
}

Swift 3.0 version:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.normalTap))
let longGesture = UILongPressGestureRecognizer(target: self, action: Selector(("longTap:")))
tapGesture.numberOfTapsRequired = 1
myBtn.addGestureRecognizer(tapGesture)
myBtn.addGestureRecognizer(longGesture)

func normalTap(){

    print("Normal tap")
}

func longTap(sender : UIGestureRecognizer){
    print("Long tap")
    if sender.state == .ended {
        print("UIGestureRecognizerStateEnded")
        //Do Whatever You want on End of Gesture
    }
    else if sender.state == .began {
        print("UIGestureRecognizerStateBegan.")
        //Do Whatever You want on Began of Gesture
    }
}

Updated syntax for Swift 5.x:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap))
button.addGestureRecognizer(tapGesture)

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap))
button.addGestureRecognizer(longGesture)

@objc func normalTap(_ sender: UIGestureRecognizer){
    print("Normal tap")
}

@objc func longTap(_ sender: UIGestureRecognizer){
    print("Long tap")
    if sender.state == .ended {
        print("UIGestureRecognizerStateEnded")
        //Do Whatever You want on End of Gesture
    }
    else if sender.state == .began {
        print("UIGestureRecognizerStateBegan.")
        //Do Whatever You want on Began of Gesture
    }
}
Rashwan L
  • 38,237
  • 7
  • 103
  • 107
  • thank you it worked perfectly, but when i try two use long press it action twice, do you know why? thank you sir – Alvin Wan Jan 01 '16 at 03:15
  • Maybe it is capturing a double tap? – Marcelo Jan 01 '16 at 16:26
  • 2
    @AlvinWan, sorry for the late reply but it´s because `UILongPressGestureRecognizer` has two states, began and ended. I have update the code with an example. Notice that I have added a parameter to longTap and added a ":" to the `longGesture` `UILongPressGestureRecognizer`. – Rashwan L Jan 09 '16 at 07:45
  • What if I have more buttons... say myBtn1, myBtn2 and myBtn3? – arakweker Nov 26 '17 at 14:42
  • What if I have more buttons... say myBtn1 t/m myBtn10? Is there a way to make use of the Tag of the buttons? – arakweker Nov 26 '17 at 15:03
  • 1
    @arakweker, yes then you can use the tag for this. You can access the tag by `sender.view?.tag`. Do also note that each button must have it´s own declaration of a gesture, but you can still call the same functions. For example: `let tapForFirstButton = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))` and `let tapForSecondButton = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))`. – Rashwan L Nov 29 '17 at 06:16