这篇文章主要为大家详细介绍了iOS UICollectionView实现卡片效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
正文
iOS UICollectionView实现卡片效果
现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo
实现上我选择了使用uicollectionview ;用uicollectionviewflowlayout来定制样式;下面看看具体实现
具体实现
1、创建uicollectionview
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
- ( void )createcollectionview { cgfloat pading = 0 * screen_width/375; lhleftcollocationview * layout = [[lhleftcollocationview alloc]init]; layout.scrolldirection = uicollectionviewscrolldirectionhorizontal; layout.minimumlinespacing = pading; layout.minimuminteritemspacing = pading; // uicollectionviewflowlayout *layout = [[uicollectionviewflowlayout alloc]init]; // layout.scrolldirection = uicollectionviewscrolldirectionhorizontal; _collectionview3 = [[uicollectionview alloc] initwithframe:cgrectmake(0, 100, [uiscreen mainscreen].bounds.size.width, imageheight * screen_rate) collectionviewlayout:layout]; _collectionview3.tag = 33; _collectionview3.datasource = self; _collectionview3.delegate = self; _collectionview3.bounces = no; _collectionview3.alwaysbouncehorizontal = no; _collectionview3.alwaysbouncevertical = no; _collectionview3.backgroundcolor = [uicolor graycolor]; _collectionview3.showshorizontalscrollindicator = no; _collectionview3.showsverticalscrollindicator = no; [self.view addsubview:_collectionview3]; [_collectionview3 registerclass:[collectionviewcell class ] forcellwithreuseidentifier:collectionviewcell]; } |
2、实现具体代理方法 uicollectionviewdelegate,uicollectionviewdatasource
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{ return self.modelarray.count; } - (nsmutablearray *)modelarray { if (!_modelarray) { _modelarray = [nsmutablearray array]; } return _modelarray; } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { collmodel *infomodel = self.modelarray[indexpath.row]; nslog(@ "section:%ld --- row:%ld -----%@" ,indexpath.section,indexpath.row,infomodel.title); collectionviewcell * cell = [collectionview dequeuereusablecellwithreuseidentifier:collectionviewcell forindexpath:indexpath]; cell.itemmodel = infomodel; return cell; } // 返回每个item的大小 - (cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath { cgfloat cwidth = 80 * screen_rate; cgfloat cheight = 80 * screen_rate; return cgsizemake(cwidth, cheight); } #pragma mark - uicollectionviewdelegate点击事件 - ( void )collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath{ collmodel *infomodel = self.modelarray[indexpath.row]; nslog(@ "infomodelarray----%@" ,infomodel.title); } |
3、自定义uicollectionviewflowlayout
lhleftcollocationview.m 实现
发表评论