Please explain this Objective-C code.

ref1view.hidden = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25f];
[ref1view setAlpha:([ref1view alpha] == 1.0f) ? 0.0f : 1.0f];
[UIView commitAnimations];

Can someone explain to me how this works? In particular, this line:

[ref1view setAlpha:([ref1view alpha] == 1.0f) ? 0.0f : 1.0f];

It seems that this function will animate alpha from 0-1 and backward from 1-0, and I just don't understand the syntax. Thank!

+5
source share
2 answers

[ref1view setAlpha:([ref1view alpha] == 1) ? 0.0f : 1.0f];:

Sets the alpha value ref1viewto 1 if it is 0 or 0 if it is 1. This is called the ternary operator , abbreviation if-else.

(condition) ? conditionistrue : conditionisfalse;
+13
source

its ternary operator ... would be the same as

if(ref1view alpha == 1)
{
[ref1view setAlpha:0.0f];
}
else
{
[ref1view setAlpha:1.0f];
}
+3
source

All Articles