[英]Swift create a common extension for UIButton and UIView Color change
就我而言,我正在嘗試創建多個buttons
。 在這里,每個按鈕放置在單獨的 UIView 上。 這個按鈕就像一個基於選擇的單個部分它的標題顏色和UIView
顏色我在每個按鈕操作方法中進行更改。 在這里,我需要為所有按鈕標題和 UIView 顏色變化創建一個通用擴展。 一次,按鈕單擊需要將值傳遞給擴展或 function 以更改 colors 為selection
按鈕。 這是我正在嘗試減少代碼重復和 LOC。
NOTE:
下面我只發布了一個按鈕代碼,但我有很多按鈕。 我想讓它成為常見的 class 並傳遞值來更改 colors。 如何做到這一點?
第一個按鈕操作
@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 主按鈕:UIButton { }
將您的按鈕和視圖存儲在某種數據結構中並對其進行迭代。 創建一個 function,它將在作為參數傳遞的視圖上設置selectedColor
並在 rest 上設置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))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.