目标C需要帮助

时间:2016-05-31 03:46:37

标签: objective-c xcode macos xcode7

我正在尝试制作欢呼板,只要我的行数是奇数就行。在偶数中,它对整个第二行的颜色为1,对第一行的颜色为2。而不是第一个盒子上的color1和第二个盒子上的颜色2来回切换。

enter image description here

这是绘制矩形(正方形)的逻辑。我觉得有一些问题吗?

PanelWidth是用户输入每个框的像素大小

PanelHeight是每个用户输入的像素大小

i是列数(水平)

r是行数(垂直)

// Drawing Rectangles
bool switch1 = true;
for (int i = 0; i < col; i++) {
    long int positionHor = (panelWidth*i);
    for (int r = 0; r < row; r++) {
        int positionVer = (panelHeight*r);
        NSRect col1= NSMakeRect(positionHor,positionVer, panelWidth, panelHeight);
        if (_bgEnable) {
            if(switch1)
            {
                [rectColor set];
            }
            else {
                [rectColor2 set];
            }

        }

        else{
            [[NSColor colorWithCalibratedWhite:0.0 alpha:0.0] setFill];
            NSRectFillUsingOperation(col1, NSCompositeSourceAtop);
        }

        NSRectFill (col1);
        switch1 = !switch1;


        if (_BoarderEnable) {
        [boarderColor set];
        NSFrameRectWithWidth ( col1, _boarderWidth);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于您确定要绘制的颜色的方式

for (int r = 0; r < row; r++) 
{
  …        
  if (_bgEnable) 
  {
    if(switch1)
    {
      …
    }
  }
  …
  switch1 = !switch1;
}

如果你有一个偶数个rects,switch被反转偶数次,在前一行开头的下一行的开头有相同的结果。

您应该计算它,而不是使用布尔开关:

for (int r = 0; r < row; r++) 
{
  …        
  if (_bgEnable) 
  {
    if( (r+i) % 2 ) // <-- here
    {
      …
    }
  }
  …
}

%表示模数,第二个参数为2,它是交替的,因为2的mod为0,1,0,1,......在第一列r为零,所以颜色每行交替。