I have an array of UIImageViews (8 of them) and I want to highlight which ever one is tapped or clicked. NOTE: It doesn't have to be highlighted I am open to other way as well. As you will see in my code I tried a different way.
As you will see below I tried to access the index.layer.borderColor and then created and if else statement. next I created a UITapGuestureRecognizer function and called this in the viewDidLoad. App is crashing with an "out of bounds" error.
func setHandleTap() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
imageArray[index(ofAccessibilityElement: 0..<7)].addGestureRecognizer(tapGesture)
}
@objc func handleTap() { // this funtion checks to see what the boarder image is and chages it to other once tapped.
if imageArray[index(ofAccessibilityElement: 0..<7)].layer.borderColor == #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1).cgColor {
imageArray[index(ofAccessibilityElement: 0..<7)].layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
}else{
imageArray[index(ofAccessibilityElement: 0..<7)].layer.borderColor = #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1).cgColor
}
}
}
I call the setHandelTap() in ViewDidLoad hoping it would run allowing the image to be highlighted.
Iterating through the array should be enough for this.
Maybe something like this in your handleTap():
let desiredColor = #colorLiteral(red: 0.01680417731, green: 0.1983509958, blue: 1, alpha: 1).cgColor
for imageView in imageArray {
if imageView.layer.borderColor == desiredColor {
imageView.layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor // Sets it to Black Color.
}
}
Note: By the way there is no need for the else code because the current color is already the color you wanted to set on the else