Swift 如何优雅地处理多个按钮 UI



在我的视图中控制器我需要处理按钮UI

我认为我的处理 UI 的方法效率不高

如果有一天我需要做更多的按钮怎么办?

今天我恰好只有三个.....

只想让用户按三个中的一个,而其他人保持原始颜色

喜欢让用户感觉到他们在多个按钮中按下哪一个

这是我的@IBAction

@IBAction func btnPress (_ sender: UIButton) {
        let clickedBackgroundColor = UIColor(red: 1, green: 153, blue: 202, a: 1)
        let clickedTextColor = UIColor(red: 255, green: 255, blue: 255, a: 1)
        let originBackgroundColor = UIColor(red: 216, green: 247, blue: 250, a: 1)
        let originTextColor = UIColor(red: 0, green: 71, blue: 88, a: 1)
        switch sender {
        case btnA:
            btnA.backgroundColor = clickedBackgroundColor
            btnA.setTitleColor(clickedTextColor, for: .normal)
            btnA.underline()
            btnB.backgroundColor = originBackgroundColor
            btnB.setTitleColor(originTextColor, for: .normal)
            btnC.backgroundColor = originBackgroundColor
            btnC.setTitleColor(originTextColor, for: .normal)

        case btnB:
            btnB.backgroundColor = clickedBackgroundColor
            btnB.setTitleColor(clickedTextColor, for: .normal)
            btnB.underline()
            btnA.backgroundColor = originBackgroundColor
            btnA.setTitleColor(originTextColor, for: .normal)
            btnC.backgroundColor = originBackgroundColor
            btnC.setTitleColor(originTextColor, for: .normal)

        case btnC:
            btnC.backgroundColor = clickedBackgroundColor
            btnC.setTitleColor(clickedTextColor, for: .normal)
            btnC.underline()
            btnA.backgroundColor = originBackgroundColor
            btnA.setTitleColor(originTextColor, for: .normal)
            btnB.backgroundColor = originBackgroundColor
            btnB.setTitleColor(originTextColor, for: .normal)
        default:break
        }
    }

和我的 UIButton 扩展添加下划线

我只能添加下划线,但是如果用户单击其他按钮,如何删除它?

extension UIButton {
    func underline() {
        guard let text = self.titleLabel?.text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: text.count))
        self.setAttributedTitle(attributedString, for: .normal)
    }
}

真的很新,不擅长质疑是否需要更多信息,请问我,谢谢。

您只需为所有buttons创建一个Outlet Collection,而不是显式创建buttons array并使用它来根据选择设置button's属性,即

@IBOutlet var buttons: [UIButton]!

用法:

@IBAction func btnPress (_ sender: UIButton) {
    let clickedBackgroundColor = UIColor(red: 1, green: 153, blue: 202, a: 1)
    let clickedTextColor = UIColor(red: 255, green: 255, blue: 255, a: 1)
    let originBackgroundColor = UIColor(red: 216, green: 247, blue: 250, a: 1)
    let originTextColor = UIColor(red: 0, green: 71, blue: 88, a: 1)
    buttons.forEach { (button) in
        button.backgroundColor = (button == sender) ? clickedBackgroundColor : originBackgroundColor
        button.setTitleColor((button == sender) ? clickedTextColor : originTextColor, for: .normal)
        if button == sender {
            button.underline()
        }
    }
}

注意:此外,无需遍历array两次。 One single traversal工作正常。

最新更新