教程CGRectMake理解

时间:2013-06-19 16:25:14

标签: ios objective-c frame gesture 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 :是什么意思?

我从未见过我的经历。本声明中==? -:的含义是什么?或者你可以解释整个事情吗?如果我向左滑动,这对于框架会是什么?对不对?

非常感谢。

3 个答案:

答案 0 :(得分:1)

这是一个简短的声明,可以写成:

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

答案 1 :(得分: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);
}

答案 2 :(得分:-3)

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

在这种情况下,如果向右滑动,侧视图将向左移动并隐藏,这是通过给出负x值来实现的。