九宫格算法

//九宫格算法。

/*

计算步骤

1,确定每个app的宽和高

2,计算marginX,marginY, marginTop

3,计算每个app所在的行索引和列索引

4,根据当前app的行索引和列索引计算appX和appY

*/

-(void)scratchableLatexAlgorithm{

NSString *path = [[NSBundle mainBundle]pathForResource:@"plist文件名" ofType:nil];

NSArray *array = [NSArray arrayWithContentsOfFile:path];

//获取页面view的宽度

CGFloat viewWidth = self.view.frame.size.width;

//假设每行的应用个数

int columns = 3;

//每个应用的宽和高

CGFloat appW = 75;

CGFloat appH = 90;

CGFloat marginTop = 30;

CGFloat marginX = (viewWidth - appW * columns)/(columns + 1);

CGFloat marginY = marginX;

for (int i=0; i

UIView *view = [[UIView alloc]init];

//计算每个单元格所在的列索引

int colIndx = i%columns;

//计算每个单元格所在的行索引

int rowIdx = i/columns;

CGFloat appX = marginX + colIndx * (appW + marginX);

CGFloat appY = marginTop + rowIdx * (appH + marginY);

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

[self.view addSubview:view];

}

}

你可能感兴趣的:(九宫格算法)