calvinklein calvinklein - 7 months ago 10
Swift Question

How to make if loop which compares color?

I would like to ask how can I make an if-statement which compares color? Mine doesn't work...

UIView.animateWithDuration(10,
animations: {
self.labelTen.layer.backgroundColor = self.timeOneColor.CGColor
}
)

if(self.labelTen.backgroundColor == timeOneColor)
{
print("something")
}

Answer

If you want to specifically compare colors you can check it by following method

if (CGColorEqualToColor(self.labelTen.backgroundColor.CGColor, timeOneColor.CGColor))
{
    //Two colors are same
}

Return true if color1 is equal to color2; false otherwise.