CGRect with empty values?

1.2k views Asked by At

I generate my Views in my View Controller like this now:

-(void) generateCardViews {
int positionsLeftInRow = _CARDS_PER_ROW;
int j = 0; // j = ROWNUMBER (j = 0) = ROW1, (j = 1) = ROW2...

for (int i = 0; i < [self.gameModel.cards count]; i++) {
    NSInteger value = ((CardModel *)self.gameModel.cards[i]).value;


    CGFloat x = (i % _CARDS_PER_ROW) * 121 + (i % _CARDS_PER_ROW) * 40 + 45;

    if (j % 2) {
        x += 80; // set additional indent (horizontal displacement)
    }

    CGFloat y = j * 85 + j * 40 + 158;
    CGRect frame = CGRectMake(x, y, 125, 125);



    CardView *cv = [[CardView alloc] initWithFrame:frame andPosition:i andValue:value];

I get my Data for _CARDS_PER_ROW from a previous VC.

But now i want to leave some places empty.

Now i can generate for example:

*  *  *
*  *  *
*  *  *

But i want to generate something like this:

*  *  *
*     *
*  *  *

but i don't know how i can skip some places for my views...

EDIT:

- (void)viewDidLoad {
[super viewDidLoad];

    level1GameViewController *gvc = [self.storyboard instantiateViewControllerWithIdentifier:@"level1GameViewController"];

    gvc.CARDS_PER_ROW = 3;
    //gvc.gameStateData = gameStateData;

    [self presentViewController:gvc animated:NO completion:nil];

   }

This is what im passing from previous controller

EDIT:

*  *  *  *


*  *  *  *
*  *  *  *


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



*  *    *  *
*          * 
   *    *

all this arrangements i need for example

1

There are 1 answers

0
Sulthan On BEST ANSWER

First of all, let's put the frame calculation into a separate method:

- (CGRect)frameForCardViewAtPositionI:(NSUInteger)i positionJ:(NSUInteger)j {
    CGRect frame;
    frame.size = CGSizeMake(125, 125);
    frame.origin.x = i * frame.size.width + 45; //or whatever logic you need here
    frame.origin.y = j * frame.size.height + 158;
    return frame;
}

Now, let's specify the card distribution with a mask block, which we send as a parameter:

- (void)generateCardViews:(BOOL (^)(NSUInteger, NSUInteger)cardAllowedOnPosition {

    NSUInteger viewIndex = 0;

    for (NSUInteger index = 0; index < [self.gameModel.cards count]; index++) {
       NSInteger value = ((CardModel *)self.gameModel.cards[index]).value;

       NSUInteger i, j;

       do {
          i = viewIndex % _CARDS_PER_ROW;
          j = viewIndex / _CARDS_PER_ROW;
          viewIndex++;
       } while (!cardAllowedOnPosition(i, j));

       CGRect frame = [self frameForCardViewAtPositionI:i positionJ:j];
       CardView *cv = [[CardView alloc] initWithFrame:frame
                                          andPosition:index
                                             andValue:value];
    }

}

Called as

[self generateCardViews:^(NSUInteger i, NSUInteger j) {
   return !(j == 1 && i > 1 && i < 4);
}];

To generate (assuming _CARDS_PER_ROW is 6)

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