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

标签 ios swift xcode uibutton uicolor

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

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

@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 时,一切正常;但是,每当我按下 button2 时,两个按钮都是 UIColor.lightGrey。我是否遗漏了一些明显的东西?

最佳答案

您可以“免费”获得一些带有按钮的方法来管理它们的状态。其中之一是 isSelected。另一个是 tag 属性,因此您可以确定哪个按钮是哪个。由于您只有两个按钮,因此您只需使用 isSelected 即可确定哪个是哪个。您还可以使用计算的 vars 让您的生活更轻松。考虑到这些因素,您可以使用以下一种方法来管理按钮的状态:

  1. 像这样声明一个buttons computed var

    @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) 将在按钮的整个生命周期内保持有效,因此您无需在初始按钮之后摆弄它声明(除非您想稍后更改按钮的行为)。

  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 连接,然后将两个按钮重新连接到上述方法。

关于ios - 按下两个 UIButton 时如何更改它们的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57533518/

相关文章:

ios - Swift 3 中的标签栏按钮颜色?

ios - 使用 UIPageViewController 使 UI ScrollView 滚动

ios - 如何将键及其值添加到字典中?

swift - 为自定义私有(private)类实现 Equatable - Swift

iOS:没有 cocoapods 和插件的 crashlytics 集成

objective-c - 由于未找到 "nonatomic",使用 OpenCV 的 iPhone 5.1 应用程序崩溃

ios - 如何将 NSExceptionDomains 添加到 xcode 版本 7.0.1 的 plist?

ios - 如何根据字典键过滤数组?

ios - 按字母顺序对 UItableview 中的数据进行排序的最简单方法

objective-c - NSOpenPanel 在 Objective-C 中获取文件名?