Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button tap and long press gesture

Tags:

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")     } } 
like image 494
Alvin Wan Avatar asked Dec 31 '15 15:12

Alvin Wan


People also ask

What does long press mean on Iphone?

Long-press (also known as press-and-hold) gestures detect one or more fingers (or a stylus) touching the screen for an extended period of time. You configure the minimum duration required to recognize the press and the number of times the fingers must be touching the screen.

What's long press?

A Long Press refers to pressing a physical button or tap a virtual button on a touchscreen and holding it down for a second or two. Employed on touchscreens, smartphones, tablets, and smartwatches, the long press or long tap increases the user interface's flexibility.

How do I add a button in SwiftUI?

SwiftUI's button is similar to UIButton , except it's more flexible in terms of what content it shows and it uses a closure for its action rather than the old target/action system. To create a button with a string title you would start with code like this: Button("Button title") { print("Button tapped!") }


1 Answers

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     } } 
like image 56
Rashwan L Avatar answered Oct 07 '22 22:10

Rashwan L