I'm animating my custom view of an UIBarButtonItem by rotating it by 45° (+ -> x). But my + image is fading when highlighted. When it isn't highlighted anymore it animates back (x -> +) and there is no issue. I don't know how to get a not fading image/animation when highlighted?
I have a custom button class:
class PlusButton: UIButton {
override var isHighlighted: Bool {
didSet {
if isHighlighted == true {
animateTurn45()
} else {
animateTurnNeg45()
}
}
}
func animateTurn45() {
let rotationTransform = CGAffineTransform(rotationAngle: CGFloat.pi / 4)
UIView.animate(withDuration: 0.5, animations: {
self.transform = rotationTransform
}) { (_) in
// Animation completion handler
}
}
func animateTurnNeg45() {
UIView.animate(withDuration: 0.5, animations: {
self.transform = .identity
}) { (_) in
// Animation completion handler
}
}
}
And my custom button is the customView of my UIBarButtonItem:
let plusButton = PlusButton()
plusButton.setBackgroundImage(UIImage(systemName: "plus"), for: .normal)
plusButton.menu = UIMenu(title: "", children: menuItems)
popUpPlusButton.customView = plusButton
That's the initial state of the VC:

That's the highlighted state:

And when it returns to not highlighted the animation is normal again:

I figured it out myself by changing the configuration of my custom view button. I added these lines to my custom UIButton class:
not fading image anymore