如何快速比较颜色 [英] How to compare colors in swift
本文介绍了如何快速比较颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想更改按钮状态以用于不同的状态. 我这样尝试:
I want change button background for different state. I try so:
@IBAction func addToShedulerAction(sender: UIButton) {
println(sender.backgroundColor)
if sender.backgroundColor==UIColor.redColor(){
sender.backgroundColor==UIColor.whiteColor()
}
else //if sender.backgroundColor==UIColor.whiteColor()
{
sender.backgroundColor=UIColor.redColor()
}
}
但是在第一个按钮println中打印nil,背景变为红色,在第二个按钮println中打印"Optional(UIDeviceRGBColorSpace 1 0 0 1)",并且颜色不变
but in first push button println print nil and background change to red, in second push println print "Optional(UIDeviceRGBColorSpace 1 0 0 1)" and color doesn't change
推荐答案
您不使用==
运算符比较颜色.您可以这样操作,并且需要!
来展开可选颜色:
You don't compare colors using the ==
operator. You do it like this and you need the !
to unwrap the optional color:
if sender.backgroundColor!.isEqual(UIColor.redColor()) {
}
此外,在您的赋值语句中删除无关的=
.应该是:
Also, remove the extraneous =
in your assignment statement. It should be:
sender.backgroundColor = UIColor.whiteColor()
这篇关于如何快速比较颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文