【问题标题】:Tutorial CGRectMake Understanding [duplicate]教程 CGRectMake 理解 [重复]
【发布时间】:2013-06-19 16:25:14
【问题描述】:

我正在关注一个教程,我对这行代码有点困惑......

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 : 是什么意思?

在我的经验中我从未见过它。 ==? -: 在此声明中是什么意思?或者你能解释一下整个事情吗?如果我左右滑动,框架会变成什么?

非常感谢。

【问题讨论】:

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

标签: ios objective-c frame gesture cgrectmake


【解决方案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);
}

== 只是标准的等价检查。 ? 是短格式 if 运算符的开头,由 : 完成。


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

【讨论】:

  • 这不是一个很好的例子,因为三元运算符仅用于CGRectMake 中的一个值。所以你的代码如果不是真正合适的表示正在发生的事情。
  • @rmaddy,是的,添加了一些更具代表性的内容,谢谢。
【解决方案2】:

条件中的问号 (?) 称为三元运算符。

以前?运算符,语句显示条件。后 ?运算符,第一个选择表示条件的满足,第二个表示条件的暴力。所以,基本上它是 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);
}

【讨论】:

  • 其实叫三元 ;-)
  • @MatthiasBauch:感谢您指出......!!
  • @Apurv 三元,不是三元。
  • @MatthiasBauch 实际上,它被称为条件运算符。 “三元”只是它具有3个操作数的属性。其他运算符也可以有 3 个操作数。
【解决方案3】:

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

在这种情况下,如果您向右滑动,sideView 将向左移动并被隐藏,这是通过提供负 x 值来实现的。

【讨论】:

  • >>如果我左右滑动,框架会变成什么?这就是我试图解释的。怎么回答错了?请解释一下。
猜你喜欢
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多