如何改变按下的按钮颜色?

时间:2016-12-29 09:01:32

标签: swift uibutton swift3

我有3对名为private void PickFile(string folder, string extension) { Intent intent = new Intent(Intent.ActionGetContent); intent.SetType(folder+"/"+extension); intent.AddCategory(Intent.CategoryOpenable); try { StartActivityForResult(Intent.CreateChooser(intent, "Select a file"), 0); } catch (System.Exception exAct) { System.Diagnostics.Debug.Write(exAct); } } 的按钮。最初(one,numOne),(two,numTwo),(three,numThree)的所有按钮都是tintColor。我喜欢将按钮对的颜色更改为black,如果这些按钮中的一个按钮被点击,则在点击其他对时返回blue。我可以通过以下代码来实现,但是还有其他最短路吗?

black

5 个答案:

答案 0 :(得分:6)

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 }
        }
    }
}

较短的一个XD

buttons.contains(sender) ? buttons.forEach{ $0.tintColor = .blue } : buttons.forEach{ $0.tintColor = .black }

答案 1 :(得分:2)

有多种方法可以使用标记,UIButtons数组,切换大小写,使用堆栈视图等。 这是一个使用标签和插座集合的例子。

首先创建出口集合。

@IBOutlet var btn1: [UIButton]!
@IBOutlet var btn2: [UIButton]!
@IBOutlet var btn3: [UIButton]!

然后

@IBAction func buttonClicked(_ sender: UIButton) {

    if sender.tag == 1 {
        btnClicked(clr1: UIColor.black, clr2: UIColor.blue, clr3: UIColor.blue)
    }
    if sender.tag == 2 {
        btnClicked(clr1: UIColor.blue, clr2: UIColor.black, clr3: UIColor.blue)
    }
    if sender.tag == 3 {
        btnClicked(clr1: UIColor.blue, clr2: UIColor.blue, clr3: UIColor.black)
    }

}

func btnClicked(clr1: UIColor, clr2: UIColor, clr3: UIColor) {
    for i in 0..<2 {
        btn1[i].tintColor = clr1
        btn2[i].tintColor = clr2
        btn3[i].tintColor = clr3
    }
}

答案 2 :(得分:1)

这可以是最短的格式:

@IBAction func buttonTapped(_ sender: UIButton) {
    one.tintColor = .black
    numOne.tintColor = .black
    two.tintColor = .black
    numTwo.tintColor = .black
    three.tintColor = .black
    numThree.tintColor = .black

    if sender == one || sender == numOne {
        one.tintColor = .blue
        numOne.tintColor = .blue
    } else if sender == two || sender == numTwo {
        two.tintColor = .blue
        numTwo.tintColor = .blue
    } else {
        three.tintColor = .blue
        numThree.tintColor = .blue
    }
}

或使用Switch

switch sender {
case one, numOne:
    one.tintColor = .blue
    numOne.tintColor = .blue
case two, numTwo:
    two.tintColor = .blue
    numTwo.tintColor = .blue
default:
    three.tintColor = .blue
    numThree.tintColor = .blue
}

答案 3 :(得分:1)

要缩短代码,首先需要使用按钮UIButton再声明两个outlet实例,现在在Button操作中使用这两个按钮。

var firstSelected = UIButton()
var secondSelected = UIButton()

@IBAction func buttonTapped(_ sender: UIButton) {
     self.firstSelected.tintColor = .black
     self.secondSelected.tintColor = .black
     if sender == one || sender == numOne {
          self.firstSelected = one
          self.secondSelected = numOne
     }
     else if sender == two || sender == numTwo {
          self.firstSelected = two
          self.secondSelected = numTwo              
     }
     else {
          self.firstSelected = three
          self.secondSelected = numThree              
     }
     self.firstSelected.tintColor = .blue
     self.secondSelected.tintColor = .blue
}

答案 4 :(得分:1)

或使用$ 0将其缩短为接受的答案:

allButtons.forEach {
    let color: UIColor = $0.contains(sender) ? .blue : .black
    $0.forEach { $0.tintColor = color }
}