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)



                }
sky90
  • 123
  • 7
  • What did you try? – Carlo Jun 16 '17 at 09:15
  • I tried the following. By pressing the button (by an if else construction), I check the tag of the button. In the corresponding if else statement I tried to distinguish the two cases without success. – sky90 Jun 16 '17 at 09:17
  • `func longTap()` have to be declared out of `else if ` statement,not inside `if.. else`, it will be outside the `@IBAction` method – Bhautik Ziniya Jun 16 '17 at 09:35
  • Thank you for the comment. I actually wrote the function outside the whole @IBAction. Sorry for the mistake of writing it wrongly. But it does not work anyway – sky90 Jun 16 '17 at 09:37

1 Answers1

2

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 {

    }
}
Bhautik Ziniya
  • 1,554
  • 12
  • 19
  • Thanks a lot for your answer. It does not seems to work.. I show you what I wrote in my question – sky90 Jun 16 '17 at 09:23
  • What is your desired output ? What do you want to achieve ? – Bhautik Ziniya Jun 16 '17 at 09:27
  • If I perform a long press on this button I want to store something, while if I just tap it I want to call the stored data. – sky90 Jun 16 '17 at 10:59
  • Could you please help me? – sky90 Jun 21 '17 at 11:13
  • @sky90 sure, how can I help you ? – Bhautik Ziniya Jun 21 '17 at 11:23
  • oh... thank you for coming back! I still have the same problem. I followed your advices but is still not working. I posted what I've done in my question, could you tell me what am I doing wrong? – sky90 Jun 21 '17 at 11:28
  • Your `longTap()` func is wrong. In `longTap()` function you should define what ever you want to perform on longpress gesture. not to initialize gesture itself in that. so your `longTap()` should look like below. `func longTap() { // write what ever you want to perform on longpress gesture.. }` – Bhautik Ziniya Jun 21 '17 at 11:42
  • thanks for your comment. actually I did not do that. I defined the function longTap in my viwecontroller and then in a @IBAction I took your tip by writing the longTap function instead but I get the following error: "value of type ViewController has no member longTap"... – sky90 Jun 21 '17 at 17:07