九宫格的算法分析:
Tables | Are | Cool |
---|---|---|
0 | 1 | 2 |
3 | 4 | 5 |
6 | 7 | 8 |
格子间: 间隙 = (控制器view的宽度 - 3 * 应用宽度) / 4
index 表示格子的索引,即0,1,2,3……8
for(index= 0;index < 9 ; index++)
计算行号:int row = index / 3 ; 决定y
计算列号:int col = index % 3;决定x
//算法实例:
int margin = 30;//间隙
int width = 80;//格子的宽
int height = 80;//格子的高
for (int i = 0; i <9; i++) {
int row = i/3;
int col = i%3;
UILabel * label = [[UILabel alloc]init];
label.frame = CGRectMake(20+col*(width+margin), 40+row*(height+margin), width, height);
label.backgroundColor = [UIColor redColor];
[self.view addSubview:label];
}