for循创建视图

来源:互联网 发布:人工智能股票龙头 编辑:程序博客网 时间:2024/04/28 17:17
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);    }


0 0
原创粉丝点击