zoukankan      html  css  js  c++  java
  • IOS 九宫格算法

    @interface ViewController ()
    
    @property (nonatomic,strong) NSArray *apps; //获取.plist数据
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
      
        
        //九宫格的总列数
        int totalColumns=5;
        
        //1.1个格子的尺寸
        CGFloat appW=50;
        CGFloat appH=60;
        
        //2.计算间隙 =(控制器view的宽度 -5*应用宽度)/应用宽度+1
        CGFloat margin=(self.view.frame.size.width-totalColumns*appW)/(totalColumns+1);
        
        //3.要的应用个数创建对应的格子
        
        for (int index=0; index<self.apps.count; index++)
        {
            //3.1 创建1个格子
            UIView *appView=[[UIView alloc]init];
            
            //设置背景色
            appView.backgroundColor=[UIColor redColor];
            
            //3.2计算行号和列号
            int row =index /totalColumns;
            int col =index % totalColumns;
            
            CGFloat appX =margin+ col*(appW + margin);
            CGFloat appY =margin+ row*(appH + margin); 

    appView.frame
    =CGRectMake(appX, appY, appW, appH);

    //3.3添加格子到控制器的View
    [self.view addSubview:appView];
    }
    }
  • 相关阅读:
    能用HTML/CSS解决的问题,就不要用JS
    跨域
    从输入url到页面展示到底发生了什么
    hosts 文件
    了解Web及网络基础
    hybrid
    组件化和 React
    MVVM 和 VUE
    虚拟 DOM
    ES6模块化与常用功能
  • 原文地址:https://www.cnblogs.com/liuwj/p/6413034.html
Copyright © 2011-2022 走看看