I have a method:
-(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;
}
}
}
So, my question is, how to make a horizontal displacement for each line, that the second line is displaced from 1st and 3rd for example.
EDIT:
My view with buttons now looks like this: (easy)
* * * * *
* * * * *
* * * * *
* * * * *
but in some views i want that they are placed like this:
* * * * *
* * * * *
* * * * *
* * * * *
I hope this is understandable...
Edit2:
This is now working!
But how can i make something like this with My cgrectmake:
*
* *
* * *
Edit3:
If i want to do something like this:
* * * * * *
* * * * *
* * * * * *
* * * * *
It makes this:
* * * * *
* * * * * *
* * * * *
Don't know why...
Make this easier by splitting up your code a bit. The line that creates the ButtonView
should be:
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];
This makes your code much easier to read and debug.
Now you need to adjust the x
value for every every other row.
Add this:
if (j % 2) {
x += 20; // set to whatever additional indent you want
}
So your final code becomes:
-(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;
}
}
}