Reputation: 39
I have made a button by using PaintCodes code:
class AddIconView: OUIButton {
override func draw(_ rect: CGRect) {
AddIcon.draw(frame: rect)
}
}
Then I add its class to a UIButton. The problem is, the button is not highlighted anymore after touch it, what is the best way to add a highlight to it? for example for this button:
@IBOutlet weak var addButton: AddIconView!
Thank you so much for your help in advance
Edit:
I created a custom class for it:
class OUIButton: UIButton {
override var isHighlighted: Bool {
get {
return super.isHighlighted
}
set {
if newValue {
backgroundColor = .green
}
else {
backgroundColor = .blue
}
super.isHighlighted = newValue
}
}}
For the test, I added a blue and green color. With this code, when I touch the button the background will become and stay blue. I want it to change only when it is touched and after release, it comes back to the normal state, exactly like a normal UIbutton
Upvotes: 0
Views: 181
Reputation: 39
It's solved, here is the code for anyone who want to use:
class OUIButton: UIButton {
override var isHighlighted: Bool {
get {
return super.isHighlighted
}
set {
if newValue {
UIView.animate(withDuration: 0.25, delay: 0, options:.curveEaseIn , animations: {
self.alpha = 0.5
}, completion: nil)
}
else {
UIView.animate(withDuration: 0.25, delay: 0, options:.curveEaseOut , animations: {
self.alpha = 1
}, completion: nil)
}
super.isHighlighted = newValue
}
}}
Upvotes: 0