ios11 - Swift 4 按钮淡入、淡出

标签 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

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

最佳答案

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

从 Storyboard 中将 UIButton 类型设置为自定义并将文本颜色设置为白色。

enter image description here

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

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

关于ios11 - Swift 4 按钮淡入、淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47732847/

相关文章:

swift - 将旧的 KVO 转换为 Swift 4

ios - 在函数中调用 Collection View 时返回 nil

ios - 构建时错误 swift 4 xCode 9,并在 pod 中添加了 Alamofire

iOS 11.0 - 定期扫描附近的 SSID (WiFi)

ios - ARKit 支持 iPhone SE 吗?

ios - 在 Objective-C 项目中使用 Swift 静态库时出错

ios - Xcode 9 漏洞 : Cannot find cdtool

ios - 由于使用 Xcode 9 的系统资源不足,无法启动设备

ios - 为什么我无法在 iOS 11 中设置搜索栏的文本颜色?

html - WKWebView 从文档目录加载 html 在设备上不起作用