如何在按下时突出显示按钮,如swift中的ios13计算器



我在swift项目中创建了一个按钮,现在我想让我的按钮在按下时发光,就像ios13中苹果计算器中发生的那样,我使用了button.showsTouchWhenHighlighted = true,但这不是我想要的。

您可以将adjustsImageWhenHightlight设置为true:

button.adjustsImageWhenHighlighted = true

或者,如果这还不够好,你可以通过setBackgroundImage:为.highlighted状态添加另一个图像

button.setBackgroundImage(lighBackgroundImage, for: .highlighted)

您也可以在接口生成器中执行此操作。

您必须覆盖自定义UIButton类中的isSelected属性,如下所示:

override var isSelected: Bool {
didSet {
updateState(state: isSelected)
}
}
func updateState(state: Bool) {
if state == false {
self.layer.borderWidth = 0.0
self.layer.borderColor = UIColor.red.cgColor
print("false")
} else if state == true {
self.layer.borderWidth = 3.0
self.layer.borderColor = UIColor.blue.cgColor
print("true")
}
}

最新更新