-2

I am following a tutorial and I am a bit confused on this line of code...

sideView.frame = CGRectMake(gesture.direction == UISwipeGestureRecognizerDirectionRight ? -swipedCell.frame.size.width : swipedCell.frame.size.width, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);

What does gesture.direction == UISwipeGestureRecognizerDirectionRight ? -swipedCell.frame.size.width : mean?

I have never seen it in my experience. What does the == and ? - and : mean in this statement? Or could you explain the whole thing? What would this make the frame if I swiped left, and right?

Thanks a lot.

Dummy Code
  • 1,858
  • 4
  • 19
  • 38
  • @Matthias, but I am not just asking about that, I want to know the direction that it is and what the line means. – Dummy Code Jun 19 '13 at 16:34

3 Answers3

1

It's a short form if statement and could be written:

if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
    sideView.frame = CGRectMake(-swipedCell.frame.size.width, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
} else {
    sideView.frame = CGRectMake(swipedCell.frame.size.width, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
}

== is just the standard equivalence check. ? is the start of a short form if operator and is completed by the :.


As rmaddy pointed out, the above isn't strictly what's going on, it's more like:

CGFloat x;

if (gesture.direction == UISwipeGestureRecognizerDirectionRight) {
    x = -swipedCell.frame.size.width;
} else {
    x = swipedCell.frame.size.width;
}

sideView.frame = CGRectMake(x, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
Wain
  • 118,658
  • 15
  • 128
  • 151
  • This is not a good example since the ternary operator is only used for one value within `CGRectMake`. So your code if not really a suitable representation of what is going on. – rmaddy Jun 19 '13 at 16:33
  • @rmaddy, true, added a bit which is more representative, thanks. – Wain Jun 19 '13 at 16:38
1

Question mark (?) in the condition is called a ternery operator.

Before ? operator, the statement shows condition. After ? operator, first choice says the fulfilment of condition and the second shows violence of condition. So, basically it is short form of if-else.

if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
{
    sideView.frame = CGRectMake(-swipedCell.frame.size.width, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
}
else
{
    sideView.frame = CGRectMake(swipedCell.frame.size.width, swipedCell.frame.origin.y, swipedCell.frame.size.width, swipedCell.frame.size.height);
}
Apurv
  • 17,116
  • 8
  • 51
  • 67
-3

CGRectMake's signature is CGRectMake(x, y, width, height);

In this case the sideView would move to left and be hidden if you swipe in the right direction which is achieved by giving a negative x value.

mohkhan
  • 11,925
  • 2
  • 24
  • 27
  • >>What would this make the frame if I swiped left, and right? That's what I was trying to explain. How is that wrong answer ? Please explain. – mohkhan Jun 19 '13 at 17:02