lea*_*122 0 uibutton swift swift3
我有3对按钮命名(one,numOne),(two,numTwo),(three,numThree).最初tintColor的所有按钮都是black.我想改变一对按钮的颜色,blue如果这些按钮中的一个按钮被轻敲,并且black当其他对被轻敲时返回.我可以通过以下代码来实现它,但是还有其他最短的方式吗?
@IBOutlet weak var one: UIButton!
@IBOutlet weak var two: UIButton!
@IBOutlet weak var three: UIButton!
@IBOutlet weak var numOne: UIButton!
@IBOutlet weak var numTwo: UIButton!
@IBOutlet weak var numThree: UIButton!
@IBAction func buttonTapped(_ sender: UIButton) {
if sender == one || sender == numOne
{
one.tintColor = UIColor.blue
numOne.tintColor = UIColor.blue
two.tintColor = UIColor.black
numTwo.tintColor = UIColor.black
three.tintColor = UIColor.black
numThree.tintColor = UIColor.black
}
else if sender == two || sender == numTwo
{
two.tintColor = UIColor.blue
numTwo.tintColor = UIColor.blue
one.tintColor = UIColor.black
numOne.tintColor = UIColor.black
three.tintColor = UIColor.black
numThree.tintColor = UIColor.black
}
else
{
three.tintColor = UIColor.blue
numThree.tintColor = UIColor.blue
two.tintColor = UIColor.black
numTwo.tintColor = UIColor.black
one.tintColor = UIColor.black
numOne.tintColor = UIColor.black
}
}
Run Code Online (Sandbox Code Playgroud)
var allButtons: [[UIButton]] = [[one, numOne], [two, numTwo], [three, numThree]]
func tap(_ sender: UIButton) {
allButtons.forEach { buttons in
if buttons.contains(sender) {
buttons.forEach{ $0.tintColor = .blue }
} else {
buttons.forEach{ $0.tintColor = .black }
}
}
}
Run Code Online (Sandbox Code Playgroud)
XD越短
buttons.contains(sender) ? buttons.forEach{ $0.tintColor = .blue } : buttons.forEach{ $0.tintColor = .black }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |