iOS-创建9宫格代码

来源:互联网 发布:淘宝如何带客服新人 编辑:程序博客网 时间:2024/05/09 05:22
- (void)rankWithTotalColumns:(int)totalColumns andWithAppW:(int)appW andWithAppH:(int)appH{    //总列数    int _totalColumns = totalColumns;    //view尺寸    CGFloat _appW = appW;    CGFloat _appH = appH;    //横向间隙 (控制器view的宽度 - 列数*应用宽度)/(列数 + 1)    CGFloat margin = (self.view.frame.size.width - (_totalColumns * 85)) / (_totalColumns + 1);//    for (int index = 0; index < self.appsArray.count; index++) {    for (int index = 0; index < 5; index++) {        //创建一个小框框//        UIView *appView = [[UIView alloc] init];        appView.backgroundColor = [UIColor redColor];        //创建结束//        //计算框框的位置...行号列号从0开始        //行号        int row = index / totalColumns; //行号为框框的序号对列数取商        //列号        int col = index % totalColumns; //列号为框框的序号对列数取余        // 每个框框靠左边的宽度为 (平均间隔+框框自己的宽度)        CGFloat appX = margin + col * (appW + margin);        // 每个框框靠上面的高度为 平均间隔+框框自己的高度        CGFloat appY = 30 + row * (appH + margin);        appView.frame = CGRectMake(appX, appY, _appW, _appH);        [self.view addSubview:appView];    }}
0 0
原创粉丝点击