【问题标题】:Add highlight effect to the UIButton that created by PaintCode code为 PaintCode 代码创建的 UIButton 添加高亮效果
【发布时间】:2019-03-12 00:30:05
【问题描述】:

我已经使用 PaintCodes 代码制作了一个按钮:

class AddIconView: OUIButton {
    override func draw(_ rect: CGRect) {
        AddIcon.draw(frame: rect)
   }
}

然后我将它的类添加到 UIButton。问题是,按钮在触摸后不再突出显示,添加突出显示的最佳方法是什么? 例如这个按钮:

@IBOutlet weak var addButton: AddIconView!

非常感谢您提前提供的帮助

编辑:

我为它创建了一个自定义类:

class OUIButton: UIButton {
override var isHighlighted: Bool {
    get {
        return super.isHighlighted
    }
    set {
        if newValue {
            backgroundColor = .green
        }
        else {
            backgroundColor = .blue
        }
        super.isHighlighted = newValue
    }
 }}

为了测试,我添加了蓝色和绿色。使用此代码,当我触摸按钮时,背景将变为并保持蓝色。 我希望它只有在被触摸时才会改变,释放后它会恢复到正常状态,就像普通的 UIbutton 一样

【问题讨论】:

    标签: swift uibutton paintcode


    【解决方案1】:

    已经解决了,下面是代码供大家使用:

    class OUIButton: UIButton {
        override var isHighlighted: Bool {
            get {
                return super.isHighlighted
            }
            set {
                if newValue {
                        UIView.animate(withDuration: 0.25, delay: 0, options:.curveEaseIn , animations: {
                    self.alpha = 0.5
                }, completion: nil)
            }
            else {
                UIView.animate(withDuration: 0.25, delay: 0, options:.curveEaseOut , animations: {
                    self.alpha = 1
                }, completion: nil)
            }
            super.isHighlighted = newValue
        }
    }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多