• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - CGRect 具有空值?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 07:56:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我现在像这样在我的 View Controller 中生成我的 View :

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

   }

这是我从前一个 Controller 传递的内容

编辑:

*  *  *  *


*  *  *  *
*  *  *  *


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



*  *    *  *
*          * 
   *    *

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



Best Answer-推荐答案


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

- (CGRect)frameForCardViewAtPositionINSUInteger)i positionJNSUInteger)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;
}

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

- (void)generateCardViewsBOOL (^)(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)

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

关于ios - CGRect 具有空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731580/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap