Swift 4 Button淡入,淡出

时间:2017-12-09 20:26:07

标签: ios11 swift4 xcode9

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

我正在寻找最简单和最佳实践的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

我有点失落。非常感谢你的回答。

1 个答案:

答案 0 :(得分:3)

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

将UIButton类型设置为自定义,并从故事板中将文本颜色设置为白色。

enter image description here

将UIButton的BackgroundColor设置为Tungsten(例如拿计算器app)

enter image description here

为UIButton创建自定义类,并从Identity Inspector 将customButton类设置为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)
        }
    }
}

结果:

enter image description here