1 /* 九宫格计算 */ 2 int totalCol = 5;//指定总列数 3 4 CGFloat viewW = 50;//subview宽度 5 CGFloat viewH = 50;//subview高度 6 7 CGFloat marginX = (self.view.bounds.size.width-viewW*totalCol)/(totalCol+1);//计算横向空白间隙 8 CGFloat marginY = marginX;//计算纵向空白间隙(和横向一致) 9 10 for (int i = 0; i<self.appList.count; i++) {//遍历plist中的view信息 一次在view中生成appview 11 12 int col = i%totalCol;//计算appview所在的列 13 int row = i/totalCol;//计算appview所在的行 14 15 CGFloat x = col*viewW+(col+1)*marginX;//计算appview的横坐标 16 CGFloat y = row*viewH+(row+1)*marginY;//计算appview的纵坐标 17 18 UIView *appView = [[UIView alloc] initWithFrame:CGRectMake(x, y, viewW, viewH)]; 19 appView.backgroundColor = [UIColor redColor];//设置appview 20 21 [self.view addSubview:appView];//添加到view中 22 } 23 24 /* 懒加载 */ 25 -(NSArray *)appList 26 { 27 if (!appList) { 28 NSString *path = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"plist"]; 29 appList = [NSArray arrayWithContentsOfFile:path];//加载plist方法 30 NSLog(@"%@",appList); 31 } 32 return appList; 33 }
IOS代码库
时间: 2024-11-05 20:43:29