服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - ios uicollectionview实现横向滚动

ios uicollectionview实现横向滚动

2021-06-01 15:59哈皮吖 IOS

这篇文章主要为大家详细介绍了ios uicollectionview实现横向滚动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo
实现上我选择了使用uicollectionview ;用uicollectionviewflowlayout来定制样式;下面看看具体实现

效果

ios uicollectionview实现横向滚动

实现上我选择了使用uicollectionview ;用uicollectionviewflowlayout来定制样式;下面看看具体实现

具体实现

1、viviewcontroller.m 代码实现

?
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#import "viewcontroller.h"
#import "collmodel.h"
#define screen_width [uiscreen mainscreen].bounds.size.width
#define screen_height [uiscreen mainscreen].bounds.size.height
#define screen_rate ([uiscreen mainscreen].bounds.size.width/375.0)
#import "imagecell.h"
#import "lhhorizontalpageflowlayout.h"
static nsstring * const imagec = @"imagecell";
static nsstring * const moreimagec = @"imagecell";
static const nsinteger kitemcountperrow = 5; //每行显示5个
static const nsinteger krowcount = 3; //每页显示行数
static float imageheight = 80;//cell 高度
@interface viewcontroller ()<uicollectionviewdelegate,uicollectionviewdatasource>
@property (nonatomic, strong) uicollectionview * collectionview;
@property (nonatomic, strong) nsmutablearray * modelarray;
@property (nonatomic, strong) uicollectionview * morecollectionview;
@end
@implementation viewcontroller
- (void)viewdidload {
 [super viewdidload];
 nsarray *apparray = [[self getdict] objectforkey:@"dictinfo"];
 for (int i = 0; i < apparray.count; i++) {
  nsdictionary * appdic = apparray[i];
  collmodel * model = [[collmodel alloc]init];
  model.title = [appdic objectforkey:@"title"];
  model.url = [appdic objectforkey:@"url"];
  [self.modelarray addobject:model];
 }
 [self createcollectionview];
 [self createrightcollectionview];
}
 
- (nsdictionary *)getdict {
 nsstring * string = @"{\"dictinfo\":[{\"title\":\"你好啊\",\"url\":\"1.jpeg\"},{\"title\":\"你好啊\",\"url\":\"2.jpeg\"},{\"title\":\"你好啊\",\"url\":\"3.jpeg\"},{\"title\":\"你好啊\",\"url\":\"4.jpeg\"},{\"title\":\"你好啊\",\"url\":\"5.jpeg\"},{\"title\":\"你好啊\",\"url\":\"6.jpeg\"},{\"title\":\"是很好\",\"url\":\"7.jpeg\"},{\"title\":\"你好啊\",\"url\":\"1.jpeg\"},{\"title\":\"你好啊\",\"url\":\"2.jpeg\"},{\"title\":\"你好啊\",\"url\":\"3.jpeg\"},{\"title\":\"你好啊\",\"url\":\"4.jpeg\"},{\"title\":\"你好啊\",\"url\":\"5.jpeg\"},{\"title\":\"你好啊\",\"url\":\"6.jpeg\"},{\"title\":\"是很好\",\"url\":\"7.jpeg\"},{\"title\":\"你好啊\",\"url\":\"1.jpeg\"},{\"title\":\"你好啊\",\"url\":\"2.jpeg\"},{\"title\":\"你好啊\",\"url\":\"3.jpeg\"},{\"title\":\"你好啊\",\"url\":\"4.jpeg\"},{\"title\":\"你好啊\",\"url\":\"5.jpeg\"},{\"title\":\"你好啊\",\"url\":\"6.jpeg\"},{\"title\":\"是很好\",\"url\":\"7.jpeg\"}]}";
 nsdictionary *infodic = [self dictionarywithjsonstring:string];
 return infodic;
}
 
 
-(nsdictionary *)dictionarywithjsonstring:(nsstring *)jsonstring {
 if (jsonstring == nil) {
  return nil;
 }
 nsdata *jsondata = [jsonstring datausingencoding:nsutf8stringencoding];
 nserror *err;
 nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:&err];
 if(err)
 {
  nslog(@"json解析失败:%@",err);
  return nil;
 }
 return dic;
}
 
- (nsmutablearray *)modelarray {
 if (!_modelarray) {
  _modelarray = [nsmutablearray array];
 }
 return _modelarray;
}
 
- (void)createcollectionview{
 uicollectionviewflowlayout * layout = [[uicollectionviewflowlayout alloc]init];
 layout.scrolldirection = uicollectionviewscrolldirectionhorizontal;
 layout.minimumlinespacing = 0;
 layout.minimuminteritemspacing = 0;
 _collectionview = [[uicollectionview alloc] initwithframe:cgrectmake(0, 100, [uiscreen mainscreen].bounds.size.width, imageheight * screen_rate) collectionviewlayout:layout];
 _collectionview.tag = 11;
 _collectionview.backgroundcolor = [uicolor colorwithred:186 / 255.0 green:186 / 255.0 blue:186 / 255.0 alpha:0.9];
 _collectionview.datasource = self;
 _collectionview.delegate = self;
 _collectionview.bounces = no;
 _collectionview.alwaysbouncehorizontal = yes;
 _collectionview.alwaysbouncevertical = no;
 _collectionview.showshorizontalscrollindicator = no;
 _collectionview.showsverticalscrollindicator = no;
 [self.view addsubview:_collectionview];
 [_collectionview registerclass:[imagecell class] forcellwithreuseidentifier:imagec];
}
 
- (void)createrightcollectionview{
 
 lhhorizontalpageflowlayout * layout = [[lhhorizontalpageflowlayout alloc] initwithrowcount:krowcount itemcountperrow:kitemcountperrow];
 [layout setcolumnspacing:0 rowspacing:0 edgeinsets:uiedgeinsetsmake(0, 0, 0, 0)];
 layout.scrolldirection = uicollectionviewscrolldirectionhorizontal;
 // uicollectionviewflowlayout *layout = [[uicollectionviewflowlayout alloc]init];
 // layout.scrolldirection = uicollectionviewscrolldirectionhorizontal;
 layout.minimumlinespacing = 0;
 layout.minimuminteritemspacing = 0;
 _morecollectionview = [[uicollectionview alloc] initwithframe:cgrectmake(0, 300, [uiscreen mainscreen].bounds.size.width, imageheight * screen_rate * krowcount) collectionviewlayout:layout];
 _morecollectionview.backgroundcolor = [uicolor clearcolor];
 _morecollectionview.tag = 22;
 _morecollectionview.datasource = self;
 _morecollectionview.delegate = self;
 _morecollectionview.bounces = no;
 _morecollectionview.alwaysbouncehorizontal = yes;
 _morecollectionview.alwaysbouncevertical = no;
 _morecollectionview.backgroundcolor = [uicolor colorwithred:186 / 255.0 green:186 / 255.0 blue:186 / 255.0 alpha:0.9];
 _morecollectionview.showshorizontalscrollindicator = no;
 _morecollectionview.showsverticalscrollindicator = no;
 [self.view addsubview:_morecollectionview];
 [_morecollectionview registerclass:[imagecell class] forcellwithreuseidentifier:moreimagec];
}
 
- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{
 return self.modelarray.count;
}
 
 
- (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath {
 collmodel * model = self.modelarray[indexpath.row];
 imagecell * cell = [collectionview dequeuereusablecellwithreuseidentifier:imagec forindexpath:indexpath];
 cell.itemmodel = model;
 return cell;
}
 
// 返回每个item的大小
- (cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout *)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath {
 cgfloat cwidth = imageheight * screen_rate;
 cgfloat cheight = imageheight * screen_rate;
 return cgsizemake(cwidth, cheight);
}
 
#pragma mark - uicollectionviewdelegate点击事件
- (void)collectionview:(uicollectionview *)collectionview didselectitematindexpath:(nsindexpath *)indexpath{
 collmodel * model = self.modelarray[indexpath.row];
 nslog(@"self.appmodelarray----%@",model.title);
}
 
 
- (void)didreceivememorywarning {
 [super didreceivememorywarning];
 // dispose of any resources that can be recreated.
}
 
@end

2、自定义uicollectionviewflowlayout

lhhorizontalpageflowlayout.h 实现

?
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
33
34
#import <uikit/uikit.h>
@interface lhhorizontalpageflowlayout : uicollectionviewflowlayout
/** 列间距 */
@property (nonatomic, assign) cgfloat columnspacing;
/** 行间距 */
@property (nonatomic, assign) cgfloat rowspacing;
/** collectionview的内边距 */
@property (nonatomic, assign) uiedgeinsets edgeinsets;
 
/** 多少行 */
@property (nonatomic, assign) nsinteger rowcount;
/** 每行展示多少个item */
@property (nonatomic, assign) nsinteger itemcountperrow;
 
//固定宽度
@property (nonatomic, assign) cgfloat itemwidth; //设置完这个,就会自动计算列间距
//固定高度
@property (nonatomic, assign) cgfloat itemhight;//设置完这个,就会自动计算行间距
 
/** 所有item的属性数组 */
@property (nonatomic, strong) nsmutablearray *attributesarraym;
 
/** 设置行列间距及collectionview的内边距 */
- (void)setcolumnspacing:(cgfloat)columnspacing rowspacing:(cgfloat)rowspacing edgeinsets:(uiedgeinsets)edgeinsets;
/** 设置多少行及每行展示的item个数 */
- (void)setrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow;
 
#pragma mark - 构造方法
/** 设置多少行及每行展示的item个数 */
+ (instancetype)horizontalpageflowlayoutwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow;
/** 设置多少行及每行展示的item个数 */
- (instancetype)initwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow;
 
@end

lhhorizontalpageflowlayout.m 实现

?
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#import "lhhorizontalpageflowlayout.h"
 
@implementation lhhorizontalpageflowlayout
 
#pragma mark - public
- (void)setcolumnspacing:(cgfloat)columnspacing rowspacing:(cgfloat)rowspacing edgeinsets:(uiedgeinsets)edgeinsets
{
 self.columnspacing = columnspacing;
 self.rowspacing = rowspacing;
 self.edgeinsets = edgeinsets;
}
 
- (void)setrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow
{
 self.rowcount = rowcount;
 self.itemcountperrow = itemcountperrow;
}
 
#pragma mark - 构造方法
+ (instancetype)horizontalpageflowlayoutwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow
{
 return [[self alloc] initwithrowcount:rowcount itemcountperrow:itemcountperrow];
}
 
- (instancetype)initwithrowcount:(nsinteger)rowcount itemcountperrow:(nsinteger)itemcountperrow
{
 self = [super init];
 if (self) {
  self.rowcount = rowcount;
  self.itemcountperrow = itemcountperrow;
 }
 return self;
}
 
 
#pragma mark - 重写父类方法
- (instancetype)init
{
 self = [super init];
 if (self) {
  [self setcolumnspacing:0 rowspacing:0 edgeinsets:uiedgeinsetszero];
 }
 return self;
}
 
/** 布局前做一些准备工作 */
- (void)preparelayout
{
 [super preparelayout];
 if (self.attributesarraym && self.attributesarraym.count > 0) {
  [self.attributesarraym removeallobjects];
 }
 
 // 从collectionview中获取到有多少个item
 nsinteger itemtotalcount = [self.collectionview numberofitemsinsection:0];
 
 // 遍历出item的attributes,把它添加到管理它的属性数组中去
 for (int i = 0; i < itemtotalcount; i++) {
  nsindexpath *indexpath = [nsindexpath indexpathforitem:i insection:0];
  uicollectionviewlayoutattributes *attributes = [self layoutattributesforitematindexpath:indexpath];
  [self.attributesarraym addobject:attributes];
 }
}
 
/** 计算collectionview的滚动范围 */
- (cgsize)collectionviewcontentsize
{
 // 计算出item的宽度
 cgfloat itemwidth = (self.collectionview.frame.size.width - self.edgeinsets.left - self.itemcountperrow * self.columnspacing) / self.itemcountperrow;
 // 从collectionview中获取到有多少个item
 nsinteger itemtotalcount = [self.collectionview numberofitemsinsection:0];
 
 // 理论上每页展示的item数目
 nsinteger itemcount = self.rowcount * self.itemcountperrow;
 // 余数(用于确定最后一页展示的item个数)
 nsinteger remainder = itemtotalcount % itemcount;
 // 除数(用于判断页数)
 nsinteger pagenumber = itemtotalcount / itemcount;
 // 总个数小于self.rowcount * self.itemcountperrow
 if (itemtotalcount <= itemcount) {
  pagenumber = 1;
 }else {
  if (remainder == 0) {
   pagenumber = pagenumber;
  }else {
   // 余数不为0,除数加1
   pagenumber = pagenumber + 1;
  }
 }
 
 cgfloat width = 0;
 // 考虑特殊情况(当item的总个数不是self.rowcount * self.itemcountperrow的整数倍,并且余数小于每行展示的个数的时候)
 if (pagenumber > 1 && remainder != 0 && remainder < self.itemcountperrow) {
  width = self.edgeinsets.left + (pagenumber - 1) * self.itemcountperrow * (itemwidth + self.columnspacing) + remainder * itemwidth + (remainder - 1)*self.columnspacing + self.edgeinsets.right;
 }else {
  width = self.edgeinsets.left + pagenumber * self.itemcountperrow * (itemwidth + self.columnspacing) - self.columnspacing + self.edgeinsets.right;
 }
 
 // 只支持水平方向上的滚动
 return cgsizemake(width, 150);
}
 
/** 设置每个item的属性(主要是frame) */
- (uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath
{
 // item的宽高由行列间距和collectionview的内边距决定
 cgfloat itemwidth = (self.collectionview.frame.size.width) / self.itemcountperrow;
 cgfloat itemheight = (self.collectionview.frame.size.height) / self.rowcount;
 
 nsinteger item = indexpath.item;
 // 当前item所在的页
 nsinteger pagenumber = item / (self.rowcount * self.itemcountperrow);
 nsinteger x = item % self.itemcountperrow + pagenumber * self.itemcountperrow;
 nsinteger y = item / self.itemcountperrow - pagenumber * self.rowcount;
 
 // 计算出item的坐标
 cgfloat itemx = itemwidth * x;
 cgfloat itemy = itemheight * y;
 
 uicollectionviewlayoutattributes *attributes = [super layoutattributesforitematindexpath:indexpath];
 // 每个item的frame
 attributes.frame = cgrectmake(itemx, itemy, itemwidth, itemheight);
 
 return attributes;
}
 
/** 返回collectionview视图中所有视图的属性数组 */
- (nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect
{
 return self.attributesarraym;
}
 
 
#pragma mark - lazy
- (nsmutablearray *)attributesarraym
{
 if (!_attributesarraym) {
  _attributesarraym = [nsmutablearray array];
 }
 return _attributesarraym;
}
@end

4、自定义cell 和model

model

?
1
2
3
4
5
6
7
#import <foundation/foundation.h>
 
@interface collmodel : nsobject
@property (nonatomic,strong)nsstring *imgurl;
@property (nonatomic,strong)nsstring *title;
@property (nonatomic,strong)nsstring *url;
@end

cell 自定义

 

?
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#import "imagecell.h"
// 屏幕比例
#define screen_rate  ([uiscreen mainscreen].bounds.size.width/375.0)
 
@interface imagecell()
@property (nonatomic, strong) uiimageview *itemicon;
@end
 
@implementation imagecell
 
@synthesize itemmodel = _itemmodel;
 
- (instancetype)initwithframe:(cgrect)frame{
 if (self = [super initwithframe:frame]) {
  self.contentview.backgroundcolor = [uicolor clearcolor];
  [self initview];
 }
 return self;
}
 
- (void)initview{
 _itemicon = [[uiimageview alloc] init];
 [self.contentview addsubview:_itemicon];
 _itemicon.backgroundcolor = [uicolor clearcolor];
 cgfloat iconwidth = 80 * screen_rate;
 _itemicon.frame = cgrectmake(0, 0, iconwidth, iconwidth);
 _itemicon.center = self.contentview.center;
}
 
- (collmodel *)itemmodel{
 return _itemmodel;
}
 
- (void)setitemmodel:(collmodel *)itemmodel
{
 if (!itemmodel) {
  return;
 }
 _itemmodel = itemmodel;
 
 [self setcellwithmodel:_itemmodel];
}
 
- (void)setcellwithmodel:(collmodel *)itemmodel{
 [[nsoperationqueue mainqueue] addoperationwithblock:^{
  _itemicon.image = [uiimage imagenamed:itemmodel.url];
 }];
}

github下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u013983033/article/details/83095126

延伸 · 阅读

精彩推荐
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28