【问题标题】:CGRectMake 4 Lines with horizontal displacement for each line?CGRectMake 4行,每行都有水平位移?
【发布时间】:2015-05-06 00:04:21
【问题描述】:

我有一个方法:

-(void) generateButtons {
int positionsLeftInRow = BUTTONS_PER_ROW;
int j = 0;

for (int i = 0; i < [self.Model.buttons count]; i++) {
    NSInteger value = ((Model *)self.Model.buttons[i]).value;

    ButtonView *cv = [[ButtonView alloc] initWithFrame:CGRectMake((i % BUTTONS_PER_ROW) * 121 + (i % BUTTONS_PER_ROW) * 40 + 205, j * 122 + j * 40 + 320, 125, 125) andPosition:i andValue:value];

    if (!((Model *)self.Model.buttons[i]).outOfPlay) {
        [self.boardView addSubview:cv];


        if ([self.Model.usedButtons containsObject: self.Model.buttons[i]]) {

            [self.usedButtons addObject: cv];

            [cv flip];

        }
    }

    if (--positionsLeftInRow == 0) {
        j++;
        positionsLeftInRow = BUTTONS_PER_ROW;
    }
}

}

所以,我的问题是,如何为每一行进行水平位移,例如第二行从第一行和第三行移位。

编辑:

我的按钮视图现在看起来像这样:(简单)

*  *  *  *  *
*  *  *  *  *
*  *  *  *  * 
*  *  *  *  *

但在某些视图中,我希望它们像这样放置:

*  *  *  *  *  
 *  *  *  *  *
*  *  *  *  *  
 *  *  *  *  *

我希望这是可以理解的......

编辑2:

现在可以了!

但是我怎样才能用我的 cgrectmake 制作这样的东西:

  *
 * *
* * *

编辑3:

如果我想做这样的事情:

*  *  *  *  *  *
 *  *  *  *  *
*  *  *  *  *  *
 *  *  *  *  *

它使这个:


 *  *  *  *  *
*  *  *  *  *  *
 *  *  *  *    *  

不知道为什么……

【问题讨论】:

  • 你能粗略地描述一下你希望这些的外观并将其添加到问题中吗?您还应该在您的问题中向我们提供更多详细信息,因为很难确切知道您想要什么。你想要线条还是盒子?什么是 ButtonView?

标签: ios objective-c cgrectmake


【解决方案1】:

通过稍微拆分您的代码使这更容易。创建ButtonView 的行应该是:

CGFloat x = (i % BUTTONS_PER_ROW) * 121 + (i % BUTTONS_PER_ROW) * 40 + 205;
CGFloat y = j * 122 + j * 40 + 320;
CGRect frame = CGRectMake(x, y, 125, 125);
ButtonView *cv = [[ButtonView alloc] initWithFrame:frame andPosition:i andValue:value];

这使您的代码更易于阅读和调试。

现在您需要每隔一行调整一次x 值。

添加这个:

if (j % 2) {
    x += 20; // set to whatever additional indent you want
}

所以你的最终代码变成:

-(void) generateButtons {
    int positionsLeftInRow = BUTTONS_PER_ROW;
    int j = 0;

    for (int i = 0; i < [self.Model.buttons count]; i++) {
        NSInteger value = ((Model *)self.Model.buttons[i]).value;

        CGFloat x = (i % BUTTONS_PER_ROW) * 121 + (i % BUTTONS_PER_ROW) * 40 + 205;
        if (j % 2) {
            x += 20; // set to whatever additional indent you want
        }
        CGFloat y = j * 122 + j * 40 + 320;
        CGRect frame = CGRectMake(x, y, 125, 125);
        ButtonView *cv = [[ButtonView alloc] initWithFrame:frame andPosition:i andValue:value];

        if (!((Model *)self.Model.buttons[i]).outOfPlay) {
            [self.boardView addSubview:cv];

            if ([self.Model.usedButtons containsObject: self.Model.buttons[i]]) {
                [self.usedButtons addObject: cv];
                [cv flip];
            }
        }

        if (--positionsLeftInRow == 0) {
            j++;
            positionsLeftInRow = BUTTONS_PER_ROW;
        }
    }
}

【讨论】:

  • 非常感谢,它真的更容易阅读!工作完美。但现在我有第二个问题。如果我想要 4 行不同数量的按钮,我该怎么办?例如:第一行1 第二行2 第三行4?我必须定义一个 BUTTONS_FOR_ROW1/2/3/4 吗?这些代码会是什么样子?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 2014-09-22
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多