Search code examples
iosswiftuitapgesturerecognizer

How to pass UIImage in tap gesture


problem os to pass image in gesture that not work, i want to pass image with tap gesture and that image is used in other purpose..

func addGestureToImageView(imgView:UIImageView) {
let tapGesture1 = UITapGestureRecognizer(target: self, action:#selector(self.tapImage(tap: ,img:imageview)))
    imgView.addGestureRecognizer(tapGesture1)
}

that function call in tap gesture..

func tapImage(tap:UITapGestureRecognizer,img:UIImage) {
   viewDisplay(img:img)
}

i want to use that image..please provide a solution.


Solution

  • you can directly get the image from imageview

      func tapImage(_tap:UITapGestureRecognizer) {
    
       if let getImage = tap.view as! UIImageView
        {
              viewDisplay(getImage:img)
          }
    
    }
    

    for sample

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self. tapImage(_:)))
    imgView.isUserInteractionEnabled = true
    imgView.addGestureRecognizer(tapGesture)
    

    and handle the action as

    func tapImage(_ tap: UITapGestureRecognizer) {
    
       if let getImage = tap.view as! UIImageView
        {
              viewDisplay(getImage:img)
          }
    
    }