1

I have implement UITapGestureRecognizer and UILongPressGestureRecognizer on button click. My problem is when i tapped first time on button its not working but second time when i press the button its start working.

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
    longGesture.minimumPressDuration = 0.5
    button.addGestureRecognizer(longGesture)
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
    tapGesture.delegate = self
    tapGesture.numberOfTouchesRequired = 1
    tapGesture.numberOfTapsRequired = 1
    button.addGestureRecognizer(tapGesture) 
 @objc func longTap(_ sender: UIGestureRecognizer){
    print("long tap")

}
@objc func normalTap(_ sender: UIGestureRecognizer){

    print("Normal tap")
}
IOS Singh
  • 617
  • 6
  • 15
akshay
  • 765
  • 6
  • 20

1 Answers1

0

Try this

class ViewController: UIViewController, UIGestureRecognizerDelegate {

@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
     super.viewDidLoad()
     // Do any additional setup after loading the view, typically from a nib.
     let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:)))
     longGesture.minimumPressDuration = 0.5
     button.addGestureRecognizer(longGesture)
 }


 @IBAction func buttonClicked(_ sender: Any) {
     let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:)))
     tapGesture.delegate = self
     tapGesture.numberOfTouchesRequired = 1
     tapGesture.numberOfTapsRequired = 1
     button.addGestureRecognizer(tapGesture)
 }

 @objc func longTap(_ sender: UIGestureRecognizer){
     print("long tap")
 }

 @objc func normalTap(_ sender: UIGestureRecognizer){
     print("Normal tap")
   }
}
chirag90
  • 2,211
  • 1
  • 22
  • 37
  • make sense bro. I have placed both the gestures in view did load then only works fine .Thanks for the clue .. Place these two gesture in view did load function so that i can voteup your answer. – akshay Sep 06 '17 at 13:04