I have 5 different UIViews
in a View Controller
, and I want to highlight only the last one that was tapped, by changing its border color.
I made a Custom Highlighting Class
that encapsulates the desired effect in terms of design, and gave each UIView a UITapGestureRecognizer
with its #selector(toggleFunction)
.These 5 UIViews
already have a class assigned to them, so basically I need to change the Original Class
to the Custom Class
, or just turn it on/off as needed.
My question is how can I toggle this Custom Class on and off as I tap between the 5 UIViews
?
You can try
extension UIView {
func addBorder(_ add:Bool) {
self.layer.borderColor = add ? UIColor.red.cgColor : UIColor.green.cgColor
self.layer.borderWidth = add ? 5 : 0
}
}
@objc func tapped(_ v:UITapGestureRecognizer) {
let currentView = v.view!
allViews.forEach {
$0.addBorder($0 == currentView)
}
}
suppose you have
var allViews = [UIView]()