for循创建视图
2015-07-19 13:55
316 查看
1.for循环之 九宫格算法 //总列数 int count = 4; //小格格尺寸 int w = 50; int h = 50; //算间隙 int jianXi = (self.view.frame.size.width - count * w) / 4; for (int i = 0; i < 33; i ++) { /* 九宫格算法: 1.间隙算法 间隙 =(屏宽 - 总列数 * 小格子宽)/ 4 2.计算行号和列号 行号 = i / 总列数 列号 = i % 总列数 3.小格子X和Y轴 X = 左边的间隙也就是X + 上面列号 * (小格子的宽 + 左边间隙) Y = 上面的间隙也就是Y + 上面行号 * (小格子的高 + 上面间隙) */ //计算行号和列号,然后计算 x y值 int x = 20 + (i % count) * (w + jianXi); int y = 25 + (i / count) * (h + jianXi); UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)]; [self.view addSubview:view]; view.backgroundColor = [UIColor redColor]; }
2.for循环之 添加按钮 //总列数 int count = 2; //小格格尺寸 int w = 150; int h = 80; //算间隙 int jianXi = (bview.frame.size.width - count * w) / 4; for (int i = 0; i < arr.count; i ++) { //计算行号和列号,然后计算 x y值 int x = 28 + (i % count) * (w + jianXi); int y = 20 + (i / count) * (h + jianXi); UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(x, y, w, h); UIImage *image = [arr objectAtIndex:i]; [btn setImage:image forState:UIControlStateNormal]; [bview addSubview:btn]; }
4.for循环之 添加tabBar按钮 for (int i = 0; i < btnArray.count; i ++) { //取出图片 NSString *btnImage = btnArray[i]; //定义按钮对象 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(i*375/btnArray.count, 0, 375/btnArray.count, 49); [btn setImage:[UIImage imageNamed:btnImage] forState:UIControlStateNormal]; //按钮闪烁效果 btn.showsTouchWhenHighlighted = YES; [backGrundView addSubview:btn]; btn.tag = 100 + i; [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; //设定选中图片 selecteImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375/btnArray.count, 49)]; selecteImage.image = [UIImage imageNamed:@"home_bottom_tab_arrow"]; [backGrundView addSubview:selecteImage]; } }
7.循环添加按钮
/** * 循环创建视图 * * @param viewCount 要创建几个视图 * @param viewColor 视图颜色 * @param superView 这个视图的父视图 * @param viewMargin 视图间隙 * @param viewSize 视图大小 * @param viewY 视图Y值 * @param toView 加到哪个视图上 */ - (void)LoopCreatViewWithCount:(NSInteger)viewCount andViewColor:(UIColor *)viewColor andSuperView:(UIView *)superView andViewMargin:(CGFloat)viewMargin andViewSize:(CGSize)viewSize andViewY:(CGFloat)viewY andToView:(UIView *)toView { NSInteger count = viewCount; for (int i = 0; i<count; i++) { // 1.创建按钮 UIView *subView = [[UIView alloc] init]; subView.backgroundColor = viewColor; // 2.设置frame CGFloat viewWeight = superView.frame.size.width; // 按钮的尺寸 CGFloat w = viewSize.width; CGFloat h = viewSize.height; // 最左边的间距 = 0.5 * (控制器view的宽度 - 按钮个数 * 按钮宽度 - (按钮个数 - 1) * 按钮之间的间距) CGFloat leftMargin = (viewWeight - count * w - viewMargin * (count - 1)) * 0.5; // 按钮的x = 最左边的间距 + i * (按钮宽度 + 按钮之间的间距) CGFloat x = leftMargin + i * (w + viewMargin); CGFloat y = 200; subView.frame = CGRectMake(x, y, w, h); // 5.2.4.添加 [toView addSubview:subView]; } }
将按钮图片和线图片分别放到两个数组
CGFloat lineCount = 2;
CGFloat lineW = 2;
CGFloat lineH = self.frame.size.height;
CGFloat lineY = 0;
CGFloat btnY = 0;
CGFloat btnH = self.frame.size.height;
CGFloat btnW = (self.frame.size.width - lineCount * lineW) / 3;
// 按钮
for (int i = 0; i < self.btnArray.count; i++) {
UIButton *btn = self.btnArray[i];
CGFloat btnX = i * (btnW + 2);
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
}
// 线
for (int i = 0; i<self.lineArray.count; i++) {
UIImageView *line = self.lineArray[i];
UIButton *btn = self.btnArray[i];
CGFloat lineX = CGRectGetMaxX(btn.frame);
line.frame = CGRectMake(lineX, lineY, lineW, lineH);
}
相关文章推荐
- asp中用for循环的一个小技巧
- javascript for循环从入门到偏门(效率优化+奇特用法)
- js使用for循环查询数组中是否存在某个值
- js 数组的for循环到底应该怎么写?
- JS 使用for循环遍历子节点查找元素
- For循环中分号隔开的3部分的执行顺序探讨
- js使用for循环及if语句判断多个一样的name
- JavaScript在for循环中绑定事件解决事件参数不同的情况
- js for循环,为什么一定要加var定义i变量
- iOS开发之路--微博“更多”页面
- shell for循环与数组应用介绍
- 一个shell for循环与case结合的脚本(监控程序状态)
- Lua中的for循环和迭代器的秘密探究
- JS数组的遍历方式for循环与for...in
- Javascript基础教程之for循环
- java中for循环删除集合陷阱
- Java利用for循环输出空心菱形的实例代码
- Java for循环的几种用法分析
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string