可以滚动的集合视图

这个demo写的是将集合视图添加到滚动视图中,实现可以滑动的效果。

                  (效果图)

1.创建视图,定义集合视图和滚动视图

 1 #import "ViewController.h"
 2 #import "MyCollectionViewCell.h"//自定义的一个collectionViewCell
 3 #define MainWidth  [UIScreen mainScreen].bounds.size.width
 4 #define MainHeight [UIScreen mainScreen].bounds.size.height
 5 
 6 @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
 7 @property (nonatomic,strong)UICollectionView * collectView;
 8 @property (nonatomic,strong)UIScrollView * scrollView;
 9 @property (nonatomic,strong)UICollectionViewFlowLayout * collectVFL;
10 @end

2.初始化我们定义的集合视图和滚动视图

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3      //设置底部滚动效果_1
 4     self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,MainHeight-64-55, MainWidth-55 ,55)];//设定滚动视图显示在屏幕中的尺寸
 5     self.scrollView.backgroundColor = [UIColor clearColor];
 6     self.scrollView.bounces = NO;
 7     self.scrollView.contentSize = CGSizeMake((MainWidth-55)/3 * 5 , 55);//滚动视图的总尺寸
 8     [self.view addSubview:self.scrollView];
 9 
10     //设置底部滚动效果_2
11    //UICollectionViewFlowLayout就是为集合视图布局,它继承于UICollectionViewLayout(相关属性就不再这一一列举,请自行查找官方文档)
12     UICollectionViewFlowLayout * collectVFL = [[UICollectionViewFlowLayout alloc]init]; 
13     collectVFL.itemSize = CGSizeMake((MainWidth-55)/3, 55);
14     collectVFL.minimumLineSpacing = 0;
15     collectVFL.minimumInteritemSpacing = 0;
16     collectVFL.scrollDirection = UICollectionViewScrollDirectionHorizontal;
17     self.collectVFL = collectVFL;
18     
19     UICollectionView * collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0,(MainWidth-55)/3*5 ,55)     collectionViewLayout:collectVFL];
20     self.collectView = collectView;
21     collectView.backgroundColor = [UIColor whiteColor];
22     collectView.delegate =  self;
23     collectView.dataSource = self;
24     collectView.scrollEnabled =NO;
25     //注意点:集合视图在初始化后需要注册
26     [collectView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
27     //将集合视图添加在滚动视图上
28     [self.scrollView addSubview:collectView];
29     
30     [self.view setBackgroundColor:[UIColor grayColor]];
31 
32 }

3.实现集合视图的代理方法

 1 #pragma mark - 集合视图代理
 2 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
 3     return  5;
 4 }
 5 
 6 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
 7     
 8     static NSString * ide = @"cell";
 9     MyCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ide forIndexPath:indexPath];
10     
11     cell.titleLabel.text = @"哈哈哈";
12     cell.backgroundColor = [UIColor clearColor];
13     cell.photoImage.image =  [UIImage imageNamed:@"运行状态@2x"];
14     return cell;
15 }
16 
17 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
18         NSLog(@"点击的Item:%ld",indexPath.item);
19 }

4.结束! 此文是小编第一次书写博客,有不足的地方还请大神指点改正。

转载于:https://www.cnblogs.com/Casey-zz/p/5653292.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值