如何在按下按钮时突出显示按钮,如swift中的ios13计算器?
我在一个swift项目中创建了一个按钮,现在我想让我的按钮在按下时发光,就像ios13中苹果计算器中发生的那样,我使用了如何在按下按钮时突出显示按钮,如swift中的ios13计算器?,ios,swift,xcode,calculator,Ios,Swift,Xcode,Calculator,我在一个swift项目中创建了一个按钮,现在我想让我的按钮在按下时发光,就像ios13中苹果计算器中发生的那样,我使用了button.showsTouchWhenHighlighted=true,但这不是我想要的。你可以设置为true: button.adjustsImageWhenHighlighted = true 或者,如果这还不够好,您可以通过以下方式为突出显示的状态添加另一个图像: 您也可以在界面生成器中执行此操作。您必须在自定义UIButton类中重写isSelected属性,如下
button.showsTouchWhenHighlighted=true
,但这不是我想要的。你可以设置为true
:
button.adjustsImageWhenHighlighted = true
或者,如果这还不够好,您可以通过以下方式为突出显示的状态添加另一个图像:
您也可以在界面生成器中执行此操作。您必须在自定义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")
}
}
一种可能的方法是重写isHighlighted属性,并根据当前值更改其didSet上的alpha
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")
}
}