我有一个方法:
-(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/