0

My swift code below places 2 different image views on a uiview controller. When the user hits a imageivew I want that specific imageview to change color. I dont know how to apply the method to multiple image views. I think you would use the sender method.

import UIKit

class ViewController: UIViewController {
var slider = UISlider()
var image1 = UIImageView()
var image2 = UIImageView()
var with = 80

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    [slider,image1,image2].forEach{
        $0.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview($0)
        $0.backgroundColor = .systemOrange
    }

    slider.frame = CGRect(x: view.center.x-115, y: view.center.y+200, width: CGFloat(with), height: 30)
    image1.frame = CGRect(x: view.center.x-115, y: view.center.y, width: CGFloat(with), height: 30)
    image2.frame = CGRect(x: view.center.x-115, y: view.center.y-200, width: CGFloat(with), height: 30)

    slider.minimumValue = 10
    slider.maximumValue = 150



    image1.isUserInteractionEnabled = true


    let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
    image1.addGestureRecognizer(gestureRecognizer)
    image2.addGestureRecognizer(gestureRecognizer)



}


@objc func imageViewTapped(sender: UITapGestureRecognizer) {
    if let imageView = sender.view as? UIImageView {

        imageView.backgroundColor = .yellow



    }
}
}
  • 1
    You won’t get any taps on any image views if you don’t turn on user interaction! – matt Dec 16 '19 at 01:30
  • As a suggestion, I would be more descriptive in your `var` names and method signatures (ex: `ji(sender:)` – Adrian Dec 16 '19 at 01:42
  • @matt added user interaction enable –  Dec 16 '19 at 01:45
  • @Adrian changed my code with your suggestions. –  Dec 16 '19 at 03:21
  • Things are coming along, but I still don't see any code where you put a tap gesture recognizer on your image views. You seem to have had some that you commented out. `imageViewTapped` will never be called because you are not using the one thing that calls it, the tap gesture recognizer. – matt Dec 16 '19 at 03:59
  • I uncommented all the commented sections. The image2.addGestureRecognizer(gestureRecognizer) does not work if I comment it out again. image1.addGestureRecognizer(gestureRecognizer) if touch does work if image 2 is commented out. They both dont work . That is essentially my question how can both of these methods get the method attached. –  Dec 16 '19 at 04:31

2 Answers2

0

A UIGestureRecognizer is to be used with a single view. So, you need to create two seperate UITapGestureRecognizer object for two different views.

For example:-

let image1GestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image1.addGestureRecognizer(image1GestureRecognizer)

let image2GestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))   
image2.addGestureRecognizer(image2GestureRecognizer)
Nexus
  • 560
  • 1
  • 4
  • 9
  • how can I code this so that any image view that is tapped on turns yellow. Say the user uses a function to add a image view that is not declared in view did load. Hope that makes sense. Ideally not a one to one pairing. –  Dec 16 '19 at 18:56
0

I tried your code:

add below two lines to enable the user interaction as mentioned by @matt:

image1.isUserInteractionEnabled = true
image2.isUserInteractionEnabled = true

and create two separate objects for gesture:

let gestureRecognizer1 = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image1.addGestureRecognizer(gestureRecognizer1)

let gestureRecognizer2 = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
image2.addGestureRecognizer(gestureRecognizer2)

Output: enter image description here

Happy coding...

Sailendra
  • 1,318
  • 14
  • 29
  • how can I code this so that any image view that is tapped on turns yellow. Say the user uses a function to add a image view that is not declared in view did load. Hope that makes sense. Ideally not a one to one pairing. –  Dec 16 '19 at 18:56
  • @maxSmtih please check this one https://stackoverflow.com/questions/4747238/can-you-attach-a-uigesturerecognizer-to-multiple-views and this could help. Thanks – Sailendra Dec 17 '19 at 07:38