0

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)



                }
4

1 回答 1

2

您可以检查您@IBAction在情节提要中或以编程方式提供的标签,请检查以下代码。

@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 {

    }
}
于 2017-06-16T09:18:12.430 回答