Button tap and long press gesture

41,381

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
    }
}
Share:
41,381

Related videos on Youtube

Alvin Wan
Author by

Alvin Wan

Updated on November 27, 2020

Comments

  • Alvin Wan
    Alvin Wan over 3 years

    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")
        }
    }
    
  • Alvin Wan
    Alvin Wan over 8 years
    thank you it worked perfectly, but when i try two use long press it action twice, do you know why? thank you sir
  • Marcelo
    Marcelo over 8 years
    Maybe it is capturing a double tap?
  • Rashwan L
    Rashwan L over 8 years
    @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.
  • arakweker
    arakweker over 6 years
    What if I have more buttons... say myBtn1, myBtn2 and myBtn3?
  • arakweker
    arakweker over 6 years
    What if I have more buttons... say myBtn1 t/m myBtn10? Is there a way to make use of the Tag of the buttons?
  • Rashwan L
    Rashwan L over 6 years
    @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(_:))).