Uilongpressgesturerecognizer does not work with my Uiimageview

Asked

Viewed 39 times

-1

I’m trying to capture the long tap on my image. So I can send an action to in a long touch.

@IBOutlet weak var imageRecord: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(addReconigzer(press:)))
    longPressRecognizer.minimumPressDuration = 1.0
    imageRecord.addGestureRecognizer(longPressRecognizer)
}

@objc func addReconigzer(press:UILongPressGestureRecognizer){
    if press.state == .began
    {
        print("oi amigo")
    }
}

But for some reason it is not printed in my console the "hi friend" when I am pricing the image for a certain time. Can anyone tell me where I’m wrong or what I should do so I can have the action executed when I press for a long time my image?

I accept suggestions to be able to do otherwise, already tried to do with button but also not getting someone can help me ?

2 answers

3

imageview.userInteractionEnabled = true

Add this code to your viewDidLoad, I believe that this should

1


It is necessary to enable user interaction since the image is not an element of interaction initially. This can be done in both storyboard/XIB and code.

At Storyboard:

inserir a descrição da imagem aqui

Or

via code

imageRecord.userInteractionEnabled = true

Tip: Check that there is no other element on top of the image that is blocking longpress( What may be the cause of the button not working )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.