Swift 4个按钮淡入淡出

Swift 4 Button fade in, out

按下按钮时如何淡入淡出。

我正在寻找最简单和最佳实践 Swift 4 尽可能的解决方案。

类似于 Apple iOS 11 计算器按钮淡出...

到目前为止我有这个...

myButton.setTitleColor(UIColor.red, for: UIControlState.normal)
myButton.backgroundColor=UIColor.gray

myButton.setTitleColor(UIColor.gray, for: UIControlState.highlighted)
myButton.backgroundColor=UIColor.red

我有点迷路了。非常感谢您的回答。

我不知道计算器的淡入淡出 in/out 但你会得到类似的效果:

从情节提要中将 UIButton 类型设置为自定义并将文本颜色设置为白色。

将 UIButton 的 BackgroundColor 设置为 Tungsten(以计算器应用为例)

为 UIButton 创建自定义 class 并从 Identity Inspector.

将自定义按钮 class 设置为 UIButton
class customButton: UIButton {
    override open var isHighlighted: Bool {
        didSet {
            //Set colors for Highlighted & Unhighlighted
            backgroundColor = isHighlighted ? UIColor.lightGray.withAlphaComponent(0.7) : UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1.0)
        }
    }
}

结果: