您的位置:首页 > 其它

瀑布流ScrollVIew的简易逻辑思维

2016-02-29 20:49 302 查看
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
//需要一个容量
[self.view addSubview:scrollView];

// float类型的数组,里面的两个对象是0,30
float column[2] = {0,30};

for (Model *model in _totalArr)
{

////定义一个int类型的索引,根据条件判断索引的值取0还是1

int index = column[0]<=column[1]?0:1;
////根据条索引的值取0还是1 判断 x的值
float x = index?165:5;

//每次都获取 对应索引位置的最新高度
float y = column[index];
//宽度是固定的
float width = 150;
//  x/y = 150/height;
float height = [model.height floatValue]*150/[model.width floatValue];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, width, height);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundImageForState:UIControlStateNormal withURL:[NSURL URLWithString:model.picUrl]];
[scrollView addSubview:btn];

//lab x/y坐标是在btn坐标基础上计算的
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(x, y+height, 150, 44)];
lab.backgroundColor = [UIColor greenColor];
lab.numberOfLines = 0;
lab.font = [UIFont systemFontOfSize:14];
lab.textColor = [UIColor orangeColor];
lab.text = model.miaoShu;
[scrollView addSubview:lab];

// 加载完毕之后,记录高度,下次遍历的时候,数组对应的内容已经修改了
column[index] = y + height + 44 + 10;

}

// 计算scrollview的容量
float height = column[0]>=column[1]?column[0]:column[1];

scrollView.contentSize = CGSizeMake(320, height);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: