Swift 为 UIButton 和 UIView 颜色更改创建了一个通用扩展



就我而言,我正在尝试创建多个buttons。在这里,每个按钮都放置在单独的UIView上。此按钮的工作方式类似于基于选择的单个部分 它的标题颜色和UIView颜色 我在每个按钮操作方法中都在更改。在这里,我需要为所有按钮标题和 UIView 颜色更改创建一个通用扩展。一旦,按钮单击需要将值传递给扩展名或函数以更改按钮的颜色selection。这是我正在尝试减少代码重复和 LOC。

NOTE:下面我只发布了一个按钮代码,但我有很多按钮。我想使其成为通用类并传递值以更改颜色。如何实现这一点?

第一个按钮操作

@IBAction func firstButtonClick(_ sender: Any) {
self.onetimeView.backgroundColor =  colorLiteral(red: 0.184337255, green: 0.683529412, blue: 0.976475882, alpha: 1)
self.dailyView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.weeklyView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.fiftydaysView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.monthlyView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.onetimeButton.setTitleColor(UIColor.selectedColor, for: .normal)
self.dailyButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.weeklyButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.fiftydaysButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.monthlyButton.setTitleColor(UIColor.disabledColor, for: .normal)
}
@IBAction func secondButtonClick(_ sender: Any) {
self.onetimeView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.dailyView.backgroundColor =  colorLiteral(red: 0.184337255, green: 0.683529412, blue: 0.976475882, alpha: 1)
self.weeklyView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.fiftydaysView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.monthlyView.backgroundColor =  colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.onetimeButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.dailyButton.setTitleColor(UIColor.selectedColor, for: .normal)
self.weeklyButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.fiftydaysButton.setTitleColor(UIColor.disabledColor, for: .normal)
self.monthlyButton.setTitleColor(UIColor.disabledColor, for: .normal)
}
extension UIColor {
static var selectedColor = UIColor.init(red: 47/255, green: 174/255, blue: 248/255, alpha: 1)
static var disabledColor = UIColor.init(red: 170/255, green: 170/255, blue: 170/255, alpha: 1)
}

您可以创建一个子类,如下所示:

class PrimaryButton: UIButton { }

将按钮和视图存储在某种数据结构中并对其进行迭代。创建一个函数,该函数将对作为参数传递的视图设置selectedColor,对其余视图进行disabledColor

typealias Section = (UIButton, UIView)
let sections: [Section] = [
(button: onetimeButton, view: onetimeView),
(button: dailyButton, view: dailyView),
(button: weeklyButton, view: weeklyView),
(button: fiftydaysButton, view: fiftydaysView),
(button: monthlyButton, view: monthlyView),
]
func select(_ section: Section) {
sections.forEach { (section) in
section.0.setTitleColor(UIColor.disabledColor, for: .normal)
section.1.backgroundColor = UIColor.disabledColor
}
section.0.setTitleColor(UIColor.selectedColor, for: .normal)
section.1.backgroundColor = UIColor.selectedColor
}
// in UIButton click call
select((onetimeButton, onetimeView))

最新更新