Button.isHighlighted时如何更改bordercolor

Jep*_*sen 2 swift

我想知道当我UIButton点击或突出显示时,我是如何在我的边框周围改变不透明度以及文本内部的文本.

我的逻辑告诉我,它应该是这样的......但它似乎不起作用:

    //BTN STYLING

    btnstd.layer.cornerRadius = 5
    btnstd.layer.borderWidth = 1.5
    btnstd.layer.borderColor = UIColor.white.cgColor

    //Change bordercolor when highlighted
    if(btnstd.isHighlighted) {
    btnstd.layer.borderColor = UIColor(white:1,alpha:0.3).cgColor
    }
Run Code Online (Sandbox Code Playgroud)

顺便提一句,这是我的ViewDidLoad()功能

dfd*_*dfd 5

我测试了这个,希望它能满足您的需求!或者至少指出你的方向.你正在寻找的行动(我认为)是.touchDown和任何东西.touchUp.

override func viewDidLoad() {
    super.viewDidLoad()
    theButton.setTitle("Normal", for: .normal)
    theButton.setTitle("Highlighted", for: .highlighted)
    theButton.layer.borderColor = UIColor.red.cgColor
    theButton.layer.borderWidth = 1
    theButton.addTarget(self, action: #selector(startHighlight), for: .touchDown)
    theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpInside)
    theButton.addTarget(self, action: #selector(stopHighlight), for: .touchUpOutside)
}
func startHighlight(sender: UIButton) {
    theButton.layer.borderColor = UIColor.green.cgColor
    theButton.layer.borderWidth = 1
}
func stopHighlight(sender: UIButton) {
    theButton.layer.borderColor = UIColor.red.cgColor
    theButton.layer.borderWidth = 1
}
Run Code Online (Sandbox Code Playgroud)