UILongPressGestureRecognizer does not work with my UIImageView

-1

I'm trying to capture the long touch on my image. So I can send an action to you 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 the "hi friend" is not printed on my console when I'm preloading the image for a certain time. Can someone tell me where I am wrong or what I should do to be able to have the action performed when I press my image for a long time?

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

    
asked by anonymous 10.10.2018 / 15:18

2 answers

3
imageview.userInteractionEnabled = true

Add this code to your viewDidLoad , I think this should be

    
10.10.2018 / 17:51
0

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

No Storyboard:

Or

via code

imageRecord.userInteractionEnabled = true

Tip: Make sure you do not have any other elements on top of the image that is blocking the longpress. (What could be the cause of the button not working)

    
11.12.2018 / 21:31