0

I want to determine the background colour of an imageview when it is tapped and perform an action based on the colour. I cannot fathom the syntax for getting the information from the array in which the imageviews are stored. I have tried the below but it says the stated error. Any help would be appreciated. I am new and I'm sorry if I'm wasting peoples time, I'm learning by making my own app and its throwing up a lot of questions. Kind regards.

'Value of type [UIImageView] has no member backgroundColour.

@IBAction func onTapping() {
    if squares.backgroundColor == UIColor.red {
        // code    
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
JAC
  • 25
  • 6
  • 1
    squares is an array. You need to pass an index. `if squares[0].backgroundColor == .red { ` – Leo Dabus Apr 21 '20 at 17:21
  • 1
    `squares` is `[UIImageView]`, it's an Array of `UIImageView`. Are all your UIImageView on tap calling `onTapping()`? If so, add a sender, detect which imageView is tapped from it, and check then its background color. – Larme Apr 21 '20 at 17:22
  • @Larme Yes, I have 9 imageviews all linked to the same onTapping function. Thank you, I will now start googling how to add a sender and learn this new skill as well :) – JAC Apr 21 '20 at 17:25
  • https://stackoverflow.com/questions/27880607/how-to-assign-an-action-for-uiimageview-object-in-swift if you used a UITapGestureRecognizer. ie: The param will be the tap, retrieve which view was tapped from it, that's your UIImageView. But that depends on how did you implement `onTapping()`. – Larme Apr 21 '20 at 17:26
  • @JAC edit your question and show how you are detecting the tap – Leo Dabus Apr 21 '20 at 17:29
  • @LeoDabus I used story board to create the Tap Gesture Recognizer for each UIImageView and I then ctrl dragged each one onto the view controller and linked it to the onTapping function. – JAC Apr 21 '20 at 17:31
  • @JAC just add the sender parameter to your onTapping and get its view – Leo Dabus Apr 21 '20 at 17:41

1 Answers1

1

First you need to set userIteractionEnable on all your UIImageViews


enter image description here


Next change your method signature to:

@IBAction func onTapping(_ sender: UITapGestureRecognizer) { 

Then you will need to know which image view you are tapping on by checking the sender view property:

@IBAction func onTapping(_ sender: UITapGestureRecognizer) {
    let systemRed = UIView()
    systemRed.backgroundColor = .systemRed
    if sender.view?.backgroundColor == systemRed.backgroundColor {
        print("systemRed")
    } else {
        print("systemYellow")
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • You will probably have some issues detecting dynamic colors. The easiest way around would be to create a view, set its background also to the color you would like to compare to – Leo Dabus Apr 21 '20 at 18:07
  • Hi Leo. Many thanks for your continued patience and wisdom. I have done as specified, when I run the code now I get an error "viewcontroller onTapping unrecognized selector sent to instance". I will investigate as I am probably doing something foolish. – JAC Apr 21 '20 at 18:26
  • you need to collect the IBOutlet to your method. Check the circle if it is empty – Leo Dabus Apr 21 '20 at 18:28
  • The circle is full on the @IBAction onTapping line and I checked each individual tap gesture recognizer on the storyboard and they are all linked to the onTapping function. – JAC Apr 21 '20 at 18:35
  • Do I need to add a UIGestureRecognizer to the ViewDidLoad and point it at the onTapping function or this what I already did via the storyboard? – JAC Apr 21 '20 at 18:37
  • 1
    @JAC You need 9 gestures recognizers. You can add them all using the storyboard. You would need to connect the sent actions to your view controller and then click on onTapping on all of them as well as the referencing outlets to the associated image view and click on gesture recognizer that will appear there.https://www.dropbox.com/s/1gzj9i8ypnux792/Swuares.zip?dl=1 – Leo Dabus Apr 21 '20 at 18:39
  • @JAC check the link above – Leo Dabus Apr 21 '20 at 18:43
  • I have checked all of the Gesture recognizers and they all have onTapping in their sent actionsand in the Referencing Outlet Collections they all say gestureRecognizers - image view. But there is one strange thing, when I look at the Outlook inspector for the View Controller on storyboard it has a small yellow triangle which says 'The action 'onTapping' is not defined in the view controller.' – JAC Apr 21 '20 at 18:45
  • I have posted a sample project on my last comment. You can inspect it and compare to the one you are working on – Leo Dabus Apr 21 '20 at 18:51
  • 1
    Sir, you are a rare genius. It worked. I deleted each of the Tap Gesture Recognizers and re-added them, the error disappeared and it worked as expected. I am learning a great deal. Many thanks again for your help, I owe you a beer next time you're in London :) Have a good evening. – JAC Apr 21 '20 at 19:11