按下两个 UIButton 时如何更改它们的颜色?

How do I change the color of two UIButtons when I press them?

我正在尝试使用两个按钮在动画滑动视图之间切换。当加载 UIView 时,我希望按钮 1 为 UIColor.darkGrey,按钮 2 为 UIColor.lightGrey。然后,当我按下 button2 时,我希望 button2 变为 UIColor.darkGrey,而 button1 变为 UIColor.lightGrey。如果我按下按钮 1,我希望按钮 1 为 UIColor.darkGrey,按钮 2 为 UIColor.lightGrey。

看似简单;使用故事板,我为 button1 和 button2 连接了一个 UIButton 作为插座。然后我将每个连接为动作。在每个操作中,我都包含了以下代码:

@IBAction func button1Action(_ sender: UIButton) {
    button2.titleLabel?.textColor = UIColor.lightGray
    button1.titleLabel?.textColor = UIColor.darkGray
    UIView.animate(withDuration: 1){
        self.side1.constant = 0
        self.side2.constant = 0
        self.sideA.constant = 400
        self.sideB.constant = -400
        self.view.layoutIfNeeded()
    }
}
@IBAction func button2Action(_ sender: UIButton) {
    button1.titleLabel?.textColor = UIColor.lightGray
    button2.titleLabel?.textColor = UIColor.darkGray
    view.layoutIfNeeded()
    UIView.animate(withDuration: 1){
        self.side1.constant = -400
        self.side2.constant = 400
        self.sideA.constant = 0
        self.sideB.constant = 0
        self.view.layoutIfNeeded()
    }


}

当我按下按钮 1 时,一切正常;但是,每当我按下按钮 2 时,两个按钮都是 UIColor.lightGrey。我是否漏掉了一些明显的东西?

您可以使用一些方法 "for free" 来管理按钮的状态。其中之一是 isSelected。另一个是 tag 属性,所以你可以弄清楚哪个按钮是哪个。由于您只有两个按钮,因此您只需使用 isSelected 即可确定哪个是哪个。您还可以使用计算 vars 让您的生活更轻松。考虑到这些,这里有一种方法可以用来管理按钮的状态:

  1. 声明一个buttons计算变量,像这样

    @IBOutlet var firstButton: UIButton!
    @IBOutlet var secondButton: UIButton!
    
    // computed var to access your buttons
    private var buttons: [UIButton] {
        return [firstButton, secondButton]
    }
    
  2. viewDidLoad 中设置您的按钮。

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // one setup to configure each button for selected or not selected
        buttons.forEach { button in
            button.setTitleColor(.darkGray,
                                  for: .selected)
            button.setTitleColor(.lightGray,
                                  for: .normal)
        }
    
        firstButton.isSelected = true
    }
    

func setTitleColor(_ color: UIColor?, for state: UIControl.State) 将在按钮的整个生命周期内保持有效,因此您不需要在初始声明后使用它 fiddle (除非您以后想更改按钮的行为) .

  1. 两个按钮一个 @IBAction 并利用 tag 属性 找出哪个是哪个。由于您只有两个按钮,我只使用 isSelected。这是你的单身 @IBAction 的样子:

    @IBAction func buttonAction(_ sender: UIButton) {
    
        UIView.animate(withDuration: 1.0) {
            // flip buttons
            self.buttons.forEach { button in
                button.isSelected.toggle()
            }
    
            if self.firstButton.isSelected {
                // tweak your constraints for firstButton.isSelected == true
                // tweak your constraints for secondButton.isSelected == false
            } else {
                // tweak your constraints for firstButton.isSelected == false
                // tweak your constraints for secondButton.isSelected == true
            }
    
        }
    }
    

根据您当前的实施,您需要右键单击 Storyboard 上的 UIButtons 并关闭现有的 IBAction 连接,然后将两个按钮重新连接到上述方法。