2013-06-19 48 views
-2

我下面的教程,我在这行代码有点糊涂了......教程CGRectMake了解

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); 

是什么gesture.direction == UISwipeGestureRecognizerDirectionRight ? -swipedCell.frame.size.width :意思?

我从来没有见过我的经历。在本声明中==? -:是什么意思?或者你能解释一切吗?如果我向左滑动,这会构成框架,对吧?

非常感谢。

+0

@Matthias,但我不只是在问这个问题,我想知道它的方向和线的含义。 –

回答

1

这是一个简短形式的if语句,并可以写为:

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); 
} 

==只是标准等价性检查。 ?是一个简短的表格,如果操作员开始,并由:完成。


由于rmaddy指出,上述不严格是怎么回事,它更像是:

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); 
+0

这不是一个好例子,因为三元运算符只用于'CGRectMake'中的一个值。所以你的代码如果不是一个真正合适的代表什么正在发生。 – rmaddy

+0

@rmaddy,真的,加了一点更具代表性,谢谢。 – Wain

1

问号状态被称为ternery运算符(?)。

之前?该语句显示条件。之后?运营商,首选说情况的满足和第二个显示情况的暴力。所以,基本上它是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); 
} 
+1

实际上它叫**三元** ;-) –

+0

@MatthiasBauch:感谢指出它..! – Apurv

+0

@Apurv Ternary,而不是ternery。 – rmaddy

-3

CGRectMake的签名是CGRectMake(x,y,width,height);

在这种情况下,如果您向正确方向滑动(通过给出负x值实现),sideView将向左移动并隐藏。

+0

>>如果我向左滑动,这会使框架变成什么? 这就是我想解释的。那个错误的答案如何?请解释。 – mohkhan