-2

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")
}
calvinklein
  • 31
  • 1
  • 7
  • 2
    Where is the loop in your code? If this is just about comparing UIColor objects for equality check [How to compare UIColors?](http://stackoverflow.com/questions/970475/how-to-compare-uicolors) – Lasse Apr 17 '16 at 18:58
  • Can you elaborate on "doesn't work?" As it's currently written, it's difficult to tell what exactly your problem is. – Mick MacCallum Apr 17 '16 at 19:30
  • I'm just trying to compare colors but nothing works for me yet. – calvinklein Apr 17 '16 at 19:34
  • 2
    What doesn't work? It doesn't compile? It crashes? It produces unexpected results? It launches missiles? [Does Not Work](http://importblogkit.com/2015/07/does-not-work/) is not an acceptable description of your problem. – nhgrif Apr 17 '16 at 19:36
  • For example if I use "if (CGColorEqualToColor(self.labelTen.backgroundColor.CGColor, timeOneColor.CGColor))" everything is okey(there's no errors), but nothing happens not even print("something") – calvinklein Apr 17 '16 at 19:38
  • 1
    There's no print statement in your code, so I'd be surprised if it printed anything at all. What exactly is the problem? – Mast Apr 17 '16 at 19:39
  • You need to [edit your question](http://stackoverflow.com/posts/36680748/edit) to include all of the details including a reasonable description of what you expect to happen, what does happen, what the inputs are, and how your expected & actual results differ. – nhgrif Apr 17 '16 at 19:40
  • Ok guys, I found a solution. The problem was that a backgroundColor was not a CGColor. Thank you for help. – calvinklein Apr 17 '16 at 19:43

1 Answers1

1

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.

HardikDG
  • 5,892
  • 2
  • 26
  • 55