单独使用collectionCell视图

偶然间在别人工程中看到的,方法很简单下面就让我来给大家说下 简单实现使用collectionCell 创建等同于view的视图


1.自定义collectionCell 添加你需要的属性


2.自定义一个view 声明一个方法 

#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width

#define floatByScreenWidth(a) ((a)/320.0)*SCREEN_WIDTH



- (void)setView;

实现:

- (void)setView//配置cell

{

    for (int i = 0;  i < 8; i++) {

        MyCollectionCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"MyCollectionCell" owner:self options:nil]lastObject];

        cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0];

        cell.nametitle.text = [NSString stringWithFormat:@"%dcell",i];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];

        [tap addTarget:self action:@selector(ClickVideoSites:)];

        cell.tag = i+1;

        tap.view.tag = i+1;

        [cell addGestureRecognizer:tap];

        

        int x = i/8;

        if (x<4) {

            cell.frame = [self setFrameBynum:i andPage:x];

        }else{

            cell.frame = [self setFrameBynum:i andPage:x];

        }

        [self addSubview:cell];

    }

}

//布局cell的位置

-(CGRect)setFrameBynum:(int)num andPage:(int)page

{

    CGFloat length = floatByScreenWidth(74);

    CGFloat height = length/74*85;

    CGFloat width = (SCREEN_WIDTH-4*length)/5;

    

    if (num  <  4) {

        return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*num, 0, length, height);

    }

    else

        return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*(num-4),height + 10, length, height);

}



3.Controller里面使用

    _myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];

    [_myView setView];

    [self.view addSubview:_myView];


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值