Wayne Filkins Wayne Filkins - 1 year ago 63
iOS Question

How do I hide image on touch with an array of imageviews?

I have an array of image views.

var imageViewArray = [UIImageView(image: UIImage())]

I use a for loop to fill this array with images from urls. I want to make it so that when I touch one of these images it becomes hidden or alpha: 0. I tried this:

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(StoryVC.imageTapped))
newImage.userInteractionEnabled = true

And I tried adding a tag too but I can't figure out how to get the sender. I need to be able to run the function to hide the image and know which image to hide, that is the part i'm struggling with. Thanks in advance.

Answer Source

You get UITapGestureRecognizer object in your selector's parameter and it has a property view that gives you the view which has been tapped.

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.imageTapped(_:)))

func imageTapped(_ sender: UITapGestureRecognizer) {
    guard let tappedImage = sender.view else { return } 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download