I am quite new to Swift and I am fascinated to the potential of distinguish these two gesture for a button.
I am writing my first app in xCode and I am near to conclude that. As a last step I want to implement two different actions for a button depending on a long press or a tap.
I have constructed the app as follows. I have several buttons connected to one IBAction and distinguished them using tags.
coming to the tag of the one of the two buttons on which I need the long press action I don't know how to continue.
Do you have some suggestion? Thank you so much
func longTap() {
if (resultDisplay.text != ""){
storedVariableA = String(result)
eraseAll()
}
}
else if (sender.tag == 20) {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap(_:)))
longPressGesture.minimumPressDuration = 2
sender.addGestureRecognizer(longPressGesture)
}
You can check in your @IBAction
for the tag you given in storyboard or programmatically, Please check the below code.
@IBAction func action(_ sender: UIButton) {
if sender.tag == 22 { // check for your desired tag instead of "22"
// add longpress gesture. on sender // sender represents your button.
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPressGesture(_:)))
longPressGesture.minimumPressDuration = 2 // mention minimum press duration you want user to press.
sender.addGestureRecognizer(longPressGesture)
} else {
}
}