Swift 3 - Image view with Tap Gesture
I recently had an issue that seems similar to yours. I had a number of images, all of which I wanted to respond in the same way whenever the user tapped them. After some experimenting, it became clear to me that each image had to have its own UITapGestureRecognizer
instance. I ended up using code like this, which ensured that every image responded to being tapped:
for imageView in imageViews {
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapResponse))
imageView.addGestureRecognizer(tapGestureRecognizer)
imageView.isUserInteractionEnabled = true
}
Please check isUserInteractionEnabled
of UIImageView
is true
Following code may help you more with Swift 4.
As you said you want to detect image tap on tableview cell please go through this code:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.cellTappedMethod(_:)))
cell.yourImageView.isUserInteractionEnabled = true
cell.yourImageView.tag = indexPath.row
cell.yourImageView.addGestureRecognizer(tapGestureRecognizer)
And add below method to your ViewController:
@objc func cellTappedMethod(_ sender:AnyObject){
print("you tap image number: \(sender.view.tag)")
}