UI----九宫格算法

九宫格算法分析:

//添加应用信息
    
    //0.总列数(一行最多3列)
    int totalColums = 3;
    
    //1.应用的尺寸
    CGFloat appW = 85;
    CGFloat appH = 90;
    
    //2.间隙 = (控制器View的宽度 - 3 * 应用宽度)/4
    CGFloat marginX = (self.view.frame.size.width - totalColums * 85) / (totalColums+1);  //X的间距
    CGFloat marginY = 20;   //Y的间距
    
    //3.根据应用个数创建对应的框框
    for (int index = 0; index<self.apps.count; index++) {
        //3.1创建小框框
        UIView *appView = [[UIView alloc]init];
        
        //设置背景色
        appView.backgroundColor = [UIColor redColor];
        
        //3.2计算框框的位置
        
        //计算行号和列号
        int row = index / totalColums;
        int col = index % totalColums;
        
        CGFloat appX = marginX + col * (appW + marginX);
        CGFloat appY = 30 + row * (appH + marginY);
        appView.frame = CGRectMake(appX, appY, appW, appH);
        
        //3.3添加框框到控制器的View
        [self.view addSubview:appView];
    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/wutengwei007/article/details/45042365
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞