瀑布流ScrollVIew的简易逻辑思维

 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值