Why is my animation of my UIButton in swift faded/transparent when getting highlighted?

41 Views Asked by At

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: enter image description here

That's the highlighted state: enter image description here

And when it returns to not highlighted the animation is normal again: enter image description here

1

There are 1 best solutions below

0
Nikola Blazevic On

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

var configurationPlain = UIButton.Configuration.plain()
self.configuration = configurationPlain

not fading image anymore