首页 > 解决方案 > 按下时如何突出显示按钮,例如swift中的ios13计算器?

问题描述

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

标签: iosswiftxcodecalculator

解决方案


您必须覆盖自定义 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")
    }
}

推荐阅读