【问题标题】:CGRect with empty values?CGRect 具有空值?
【发布时间】:2015-06-09 12:06:51
【问题描述】:

我现在像这样在我的视图控制器中生成我的视图:

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

我从以前的 VC 获得了 _CARDS_PER_ROW 的数据。

但现在我想把一些地方留空。

现在我可以生成例如:

*  *  *
*  *  *
*  *  *

但我想生成这样的东西:

*  *  *
*     *
*  *  *

但我不知道如何跳过某些地方以获取我的观点...

编辑:

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

   }

这是我从前一个控制器传递的内容

编辑:

*  *  *  *


*  *  *  *
*  *  *  *


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



*  *    *  *
*          * 
   *    *

例如我需要的所有这些安排

【问题讨论】:

  • 展示你是如何传递数据的,这将有更多帮助。
  • 是完全随机的还是你有一个固定的位置不想出现?
  • 对于每个级别我想进行其他设置,所以在级别 1 我想要 1 行有 4 张卡片,在级别 10 我想要有 2 行有 5 对,依此类推.
  • 再次编辑我的问题以便更好地理解
  • 就在 for 循环中,创建一个 if 条件来检查是否要绘制 *

标签: ios objective-c cgrect


【解决方案1】:

首先,我们把帧计算放到一个单独的方法中:

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

现在,让我们用掩码块指定卡片分布,我们将其作为参数发送:

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

}

称为

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

生成(假设_CARDS_PER_ROW6

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

【讨论】:

    猜你喜欢
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多