UIButton with single press and long press events swift

40,651

Solution 1

If you want to perform any action with single tap you and long press the you can add gestures into button this way:

@IBOutlet weak var btn: UIButton!

override func viewDidLoad() {

    let tapGesture = UITapGestureRecognizer(target: self, #selector (tap))  //Tap function will call when user tap on button
    let longGesture = UILongPressGestureRecognizer(target: self, #selector(long))  //Long function will call when user long press on button.
    tapGesture.numberOfTapsRequired = 1
    btn.addGestureRecognizer(tapGesture)
    btn.addGestureRecognizer(longGesture)
}

@objc func tap() {

    print("Tap happend")
}

@objc func long() {

    print("Long press")
}

This way you can add multiple method for single button and you just need Outlet for that button for that..

Solution 2

@IBOutlet weak var countButton: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()

    addLongPressGesture()
}
@IBAction func countAction(_ sender: UIButton) {
    print("Single Tap")
}

@objc func longPress(gesture: UILongPressGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began {
        print("Long Press")
    }
}

func addLongPressGesture(){
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
    longPress.minimumPressDuration = 1.5
    self.countButton.addGestureRecognizer(longPress)
}
Share:
40,651

Related videos on Youtube

Kamal Upasena
Author by

Kamal Upasena

Keep calm and Code

Updated on July 09, 2022

Comments

  • Kamal Upasena
    Kamal Upasena almost 2 years

    I want to trigger two action on button click and button long click. I have add a UIbutton in my interface builder. How can i trigger two action using IBAction can somebody tell me how to archive this ?

    this is the code i have used for a button click

    @IBAction func buttonPressed (sender: UIButton) { .... }

    can i use this method or do i have to use another method for long click ?

  • Kamal Upasena
    Kamal Upasena almost 9 years
    i have multiple buttons in my ui since they all do the same this i have map them in to this method (@IBAction func buttonPressed (sender: UIButton)and can i add method programatically ?
  • Dharmesh Kheni
    Dharmesh Kheni almost 9 years
    Then you need to create outlet for all button and add this gestures into it.and this method will work for all buttons.
  • Kamal Upasena
    Kamal Upasena almost 9 years
    oh i have 30 buttons in my ui its a keyboard :( is there any way to archive them without using outlet ?
  • Dharmesh Kheni
    Dharmesh Kheni almost 9 years
    I think no. you have to create outlet and add gestures for all If you want to perform action with long press.:)
  • Kamal Upasena
    Kamal Upasena almost 9 years
    can we map a IBAction programatically ? so then i only have to create outlet for a one button only
  • Dharmesh Kheni
    Dharmesh Kheni almost 9 years
    yes you can use this button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
  • Kamal Upasena
    Kamal Upasena almost 9 years
    when i try to add the gesture i am getting error like this fatal error: unexpectedly found nil while unwrapping an Optional value any idea ?
  • Dharmesh Kheni
    Dharmesh Kheni almost 9 years
  • Samarth Kejriwal
    Samarth Kejriwal about 5 years
    @Dharmesh I am getting this issue unrecognized selector sent to instance 0x2802ec000, I am using the following code snippet for the tapFunction, @objc func longPress(_ btn : UIButton) {}