//九宮格算法箱叁。
/*
計算步驟
1惧笛,確定每個app的寬和高
2晕城,計算marginX叠纷,marginY肮蛹, marginTop
3,計算每個app所在的行索引和列索引
4创南,根據(jù)當(dāng)前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;
//假設(shè)每行的應(yīng)用個數(shù)
int columns = 3;
//每個應(yīng)用的寬和高
CGFloat appW = 75;
CGFloat appH = 90;
CGFloat marginTop = 30;
CGFloat marginX = (viewWidth - appW * columns)/(columns + 1);
CGFloat marginY = marginX;
for (int i=0; i<array.count; 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];
}
}