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



我没有使用情节提要来创建我的 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
}()

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

正常行为是,当您使用情节提要时,蓝色有色文本会慢慢更改为浅蓝色。不突兀。

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

试试这个:

var button = UIButton(type: .system)

最新更新