【问题标题】:Change button color in swift快速更改按钮颜色
【发布时间】:2015-07-02 15:39:08
【问题描述】:

如何在按下按钮时更改按钮颜色。就像在计算器应用程序中一样。当手指离开按钮时,它应该返回默认颜色。谢谢。

【问题讨论】:

    标签: iphone xcode swift ipad


    【解决方案1】:

    对于您的按钮,将颜色从默认颜色更改为 TouchDown 控件事件的突出显示颜色,并且不要在 TouchCancel 和 TouchDragExit 控件事件上将颜色恢复为默认颜色。

        button.addTarget(self, action: "changeButtonColors:", forControlEvents: .TouchDown)
        button.addTarget(self, action: "revertButtonColors:", forControlEvents: .TouchCancel)
        button.addTarget(self, action: "revertButtonColors:", forControlEvents: .TouchDragExit)
    
        func changeButtonColors(button: UIButton) {
            button.layer.backgroundColor = UIColor.greenColor().CGColor
            button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        }
    
        func revertButtonColors(button: UIButton) {
            button.layer.backgroundColor = UIColor.whiteColor().CGColor
            button.setTitleColor(UIColor.greenColor(), forState: .Normal)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      • 2014-07-11
      相关资源
      最近更新 更多