在动画其标题背景时单击 UIButton 显示错误行为

On clicking UIButton while animating its title background shows bugged behaviour

正在尝试修复按钮标题的背景。当我单击按钮时,从 blue.png 过渡到 green.png 时会发生这种情况,按钮标题的背景采用 blue.png 而按钮采用绿色。

此处整个按钮应显示相同 green.png 当我删除 UIView.transition 时效果很好。

@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
    }
}

在xib中viewcontroller:按钮背景的默认状态设置为blue.png,按钮背景的选定状态设置为green.png

您应该将 Storyboard/XIB 中的 UIButton 类型从 "System" 更改为 "Custom"。您看到的是处于 SELECTED 状态的系统按钮的默认行为。

过渡不是问题,问题是当按钮被选中时"button.isSelected = true"