ios - CGRectMake 4 条线,每条线有水平位移?

标签 ios objective-c cgrectmake

我有一个方法:

-(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;
    }
}

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

编辑:

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

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

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

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

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

编辑2:

现在可以使用了!

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

  *
 * *
* * *

编辑3:

如果我想做这样的事情:

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

它使得:


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

不知道为什么...

最佳答案

通过稍微拆分您的代码来简化此过程。创建 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;
        }
    }
}

关于ios - CGRectMake 4 条线,每条线有水平位移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065264/

相关文章:

ios - 以编程方式更改 imageview 高度 ios

ios - 如何检查是否给出了正确的链接

ios - 快速 Realm 错误

objective-c - iPad:[UIScreen mainScreen].bounds 返回错误的坐标

xcode - 如何初始化相互依赖的属性

html - 网站上的音频-可工作的跨浏览器和iPhone-具有提示

objective-c - 在 Core Data 迁移后插入数据

ios - 使用 CommonCrypto 的 NSString 类别在 RubyMotion 中返回 nil

ios - 如何获得当前的 x 和 y 位置以便我可以使用 CGRectMake?

ios - 在 Swift 3 中自动将 CGRectMake 更新为 CGRect