1

As long as you are tapping on the button, the action should be performed. As soon as you release the button, the action that is performed will be removed.

I am using a long-press Gesture but it didn't work.

let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long)) 
@objc func long() {
    topImageView.image = originalImage
    topImageView.isHidden = false
    lassoimageView.isHidden = true
    middleImageView.isHidden = true
    overlayImage.isHidden = true
    buttonTap = "Eye"
}
Nikki
  • 193
  • 13
  • This [post](https://stackoverflow.com/questions/28058082/swift-long-press-gesture-recognizer-detect-taps-and-long-press) may help. – Ars_Codicis May 25 '22 at 22:31

3 Answers3

1

You need to handle the state

@objc func long(_ tap:UILongPressGestureRecognizer) {
    switch(tap.state) {
       case .began:
           // to do
       case .ended:
           // to do
       default: break
    }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

In your code you haven’t added the gesture to your button. Like this:

myButton.addGestureRecognizer(longGesture)

Then you can handle start and events as @Sh_Khan mentioned.

Asteroid
  • 1,049
  • 2
  • 8
  • 16
0

Use .touchDown to perform action on button click

// This will start your button action.

button.addTarget(self, action: #selector(start), for: .touchDown)

// This will end your button action.

button.addTarget(self, action: #selector(end), for: .touchUpInside)

Prabha
  • 424
  • 1
  • 4
  • 11