Trying to fix the background of the button's title. And here it happens while transitioning from blue.png to green.png when I click the button the background of the title of the button takes the blue.png while button takes green.
Here the whole button should show same green.png
And when I remove UIView.transition
it works good.
@IBOutlet weak var option_1: UIButton!
func optionsAnimateGreen(){
let options = [option_1]
let stop = [stop_1]
for (option, stopper) in zip(options, stop){ //loops options and stop
let btn:UIButton = option!
if stopper{
UIView.transition(with: btn, duration: 1.5, options: [ .transitionCrossDissolve, .allowUserInteraction], animations: {
btn.toggleSelection()
}, completion: nil)
}
}
}
self.timer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(self.optionsAnimateGreen), userInfo: nil, repeats: true);
extension UIButton {
func toggleSelection() {
self.isSelected = self.isSelected ? false : true
}
}
In xib viewcontroller: The default state of button background is set to blue.png and for selected state of button background is set to green.png
You should change the UIButton type from "System" to "Custom" in your Storyboard/XIB. What you see is a default behavior from a System button in a SELECTED state.
The transition is not the problem, the problem is when the button get selected "button.isSelected = true"