Swift, ios 10 UIButton 边框

Swift, ios 10 UIButton Border

我在 Swift 3 中开发,但我在使用 UIButton 时遇到了一点问题。我会尽力解释我的问题。首先我创建了我的 UIButton:

let Button: UIButton = {
    let button = UIButton(type: .system)
    button.setBackgroundImage(UIImage(named: "button"), for: .normal)        
    button.layer.borderWidth = 2
    button.layer.borderColor = UIColor(red: 0, green: 0, blue: 80/255, alpha: 1).cgColor
    button.layer.cornerRadius = 10
    //button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

在 viewDidLoad 中我执行了操作:

  button.addTarget(self, action: #selector(buttonUnpressed), for: .touchUpInside)

其中"favButtonPressed"是一个无关紧要的函数 问题是当我触摸按钮时,边框不会像图像或文本那样不突出显示。这是一个不太重要但很烦人的细节。

我曾尝试在按下时更改 alpha 值并重新更改它,但问题是我不知道如何为其设置动画以制作 2 秒的动画(下一行代码不起作用,它确实改变了颜色但不在 2 秒内):

func buttonUnpressed(){
    UIButton.animate(withDuration: 2, animations: {
        self.button.layer.borderColor = UIColor(red: 0, green: 0, blue: 80/255, alpha: 1).cgColor
    })
}

问题;有什么方法可以在按下时向 UIButton "total behavior" 添加突出显示和取消突出显示的边框?

如果有一些表达或语法错误,请原谅我的英语。

编辑 我试过这个功能:

 func buttonUnpressed(){
    button.alpha = 0.2
    UIButton.animate(withDuration: 1) {
        self.button.alpha = 1
    }
    button.layer.borderColor = UIColor(red: 0, green: 0, blue: 80/255, alpha: 1).cgColor
}

图像有一个奇怪的亮点,但现在它或多或少起作用了。

您可以使用此扩展来动画更改边框颜色:

extension UIButton {
    func borderColorAnimation(from: CGColor, to: CGColor, duration: CFTimeInterval) {
        let animation = CABasicAnimation(keyPath: "borderColor")
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.fromValue = from
        animation.toValue = to
        animation.duration = duration
        self.layer.add(animation, forKey: "borderColor")
        self.layer.borderColor = to
    }
}

像这样:

let fromColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
let toColor = UIColor(red: 255, green: 0, blue: 0, alpha: 1).cgColor
self.button.borderColorAnimation(from: fromColor, to: toColor, duration: 200)

我确实找到了最终答案。 感谢 krotov 的帮助!

 class BorderButton: UIButton {

 override init(frame: CGRect){
    super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

var colorPast: Bool = true

override var isHighlighted: Bool {
    didSet {
        if isHighlighted && colorPast {
            borderDissapear(highlighted: isHighlighted)
            colorPast = false
        } else if !isHighlighted && !colorPast {
            borderDissapear(highlighted: isHighlighted)
            colorPast = true
        }
    }
}

func borderDissapear (highlighted: Bool) {
    let animation = CABasicAnimation(keyPath: "borderColor")
    animation.duration = 0.1
    animation.autoreverses = false
    animation.repeatCount = 1
    animation.fillMode = kCAFillModeForwards
    animation.isRemovedOnCompletion = false
    if highlighted {
        animation.fromValue = self.layer.borderColor?.copy(alpha: 1)
        animation.toValue = self.layer.borderColor?.copy(alpha: 0.3)
    } else if !highlighted {
        animation.fromValue = self.layer.borderColor?.copy(alpha: 0.3)
        animation.toValue = self.layer.borderColor?.copy(alpha: 1)
    }
    self.layer.add(animation, forKey: "borderColor")
}
}