以编程方式创建UIButton时启用色调更改动画

时间:2018-10-02 02:48:47

标签: ios swift

我没有使用Storyboard来创建UI,所以我像这样创建了按钮

var randomButton: UIButton = {
    var button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Random", for: .normal)
    button.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
    button.layer.cornerRadius = 3
    button.layer.borderWidth = 1
    button.layer.borderColor = UIColor.black.cgColor
    button.setTitleColor(.black, for: .highlighted)

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

    return button
}()

问题在于,当用户点击按钮时,在色调颜色之间没有很好的过渡/动画效果。除非用户按住按钮,否则文本的颜色不会变为黑色。

正常情况下,使用情节提要时,蓝色的文本会慢慢变为浅蓝色。不是突然。

1 个答案:

答案 0 :(得分:2)

我认为您所指的动画是系统类型按钮的一部分。

尝试一下:

var button = UIButton(type: .system)
相关问题