Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 anyone please give me a breakdown of how this works? Specifically this line:

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

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

like image 912
Hippocrates Avatar asked Jul 15 '10 14:07

Hippocrates


2 Answers

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

Sets the alpha of ref1view to be 1 if it's 0, or 0 if it's 1. This is called the ternary operator, a shorthand if-else.

(condition) ? conditionistrue : conditionisfalse;
like image 81
Jacob Relkin Avatar answered Oct 12 '22 09:10

Jacob Relkin


its a ternary operator...would be the same as

if(ref1view alpha == 1)
{
[ref1view setAlpha:0.0f];
}
else
{
[ref1view setAlpha:1.0f];
}
like image 33
Jesse Naugher Avatar answered Oct 12 '22 10:10

Jesse Naugher