0

我正在尝试调整框架。我有两个常量定义为浮点数 -viewTotalHeightExpandedviewTotalHeight. 我将两者相减得到differenceY.

  CGFloat differenceY = viewTotalHeightExpanded - viewTotalHeight;
  NSLog(@"viewTotalHeightExpanded = %f", viewTotalHeightExpanded);
  NSLog(@"viewTotalHeight = %f", viewTotalHeight);
  NSLog(@"differenceY = %f", differenceY);

日志内容如下:

2013-08-24 12:30:48.305 WS[25737:c07] viewTotalHeightExpanded = 406.000000
2013-08-24 12:30:48.305 WS[25737:c07] viewTotalHeight = 366.000000
2013-08-24 12:30:48.306 WS[25737:c07] differenceY = 680.000000

为什么differenceY不是40?

编辑

这是如何定义的viewTotalHeightviewTotalHeightExpanded

#define likeBarHeight 20.0f
#define viewTotalHeight likeBarY+likeBarHeight+sortBarHeight

所有其他链式常量,例如likeBarYsortBarHeight被定义为类似于likeBarHeight.

4

1 回答 1

5

这就是你正在做的事情:

CGFloat differenceY = viewTotalHeightExpanded - viewTotalHeight;

这是因为 viewTotalHeight 是 likeBarY+likeBarHeight+sortBarHeight 转换为

CGFloat differenceY = viewTotalHeightExpanded -likeBarY+likeBarHeight+sortBarHeight

所以试试这个:

 CGFloat differenceY = (viewTotalHeightExpanded) - (viewTotalHeight);

或者甚至更好地使 viewTotalHeight 可以在没有括号的情况下重复使用

#define viewTotalHeight (likeBarY+likeBarHeight+sortBarHeight)
于 2013-08-24T19:48:43.733 回答