瀑布流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);
相关文章推荐
- leetcode之Flatten Binary Tree to Linked List
- 航电OJ-1874畅通工程续
- 集合的toString()方法源码解析
- DFS,BFS,前序,中序,后序,层序遍历
- nios II--实验3——led 100M硬件部分
- 源码编译安装screen
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
- JAVA学习笔记——JAVA中的IO流
- Spring 源码分析(三) —— AOP(一)AOP原理
- 文本分类学习笔记(5)- KNN
- CentOS6.5下编译Hadoop-2.7.2
- notification的理解
- Vector和iterator的使用心得
- Ubuntu /bin/sh /bin/bash /bin/dash 区别
- Nginx模块 ngx_http_limit_req_module 限制请求速率
- 多元分布和狄利克雷分布
- 线程本地存储(Windows和Linux) http://blog.csdn.net/wzhwho/article/details/6427596
- HDU1051 Wooden Sticks
- YII框架的安装出现的两个小问题
- 马虎的算式(蓝桥杯)