【发布时间】:2015-07-02 15:39:08
【问题描述】:
如何在按下按钮时更改按钮颜色。就像在计算器应用程序中一样。当手指离开按钮时,它应该返回默认颜色。谢谢。
【问题讨论】:
如何在按下按钮时更改按钮颜色。就像在计算器应用程序中一样。当手指离开按钮时,它应该返回默认颜色。谢谢。
【问题讨论】:
对于您的按钮,将颜色从默认颜色更改为 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)
}
【讨论】: