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

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

服务器之家 - 编程语言 - IOS - iOS UICollectionView实现卡片效果

iOS UICollectionView实现卡片效果

2021-06-03 15:16哈皮吖 IOS

这篇文章主要为大家详细介绍了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 实现

?
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
#import "lhleftcollocationview.h"
 
@implementation lhleftcollocationview
 
 
- (cgpoint)targetcontentoffsetforproposedcontentoffset:(cgpoint)proposedcontentoffset withscrollingvelocity:(cgpoint)velocity {
 cgrect targectrect = cgrectmake(proposedcontentoffset.x, 0.0, [uiscreen mainscreen].bounds.size.width, [uiscreen mainscreen].bounds.size.height);
 nsarray * attriarray = [super layoutattributesforelementsinrect:targectrect];
 cgfloat horizontalcenterx = proposedcontentoffset.x + ([uiscreen mainscreen].bounds.size.width);
 cgfloat offsetadjustment = cgfloat_max;
 for (uicollectionviewlayoutattributes * layoutattributes in attriarray) {
 cgfloat itemhorizontalcenterx = layoutattributes.center.x;
 if (fabs(itemhorizontalcenterx-horizontalcenterx) < fabs(offsetadjustment)) {
  offsetadjustment = itemhorizontalcenterx - horizontalcenterx;
 }
 }
 return cgpointmake(proposedcontentoffset.x , proposedcontentoffset.y);
}
 
cgfloat activedistance = 400; //垂直缩放除以系数
cgfloat scalefactor = 0.50; //缩放系数 越大缩放越大
 
- (nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect {
 nsarray * array = [super layoutattributesforelementsinrect:rect];
 cgrect visiblerect = cgrectzero;
 visiblerect.origin = self.collectionview.contentoffset;
 visiblerect.size = self.collectionview.bounds.size;
 for (uicollectionviewlayoutattributes *attributes in array) {
 cgfloat distance = cgrectgetmidx(visiblerect) - attributes.center.x;
 cgfloat normalizeddistance = fabs(distance / activedistance);
 cgfloat zoom = 1 - scalefactor * normalizeddistance;
 nslog(@"zoom----%f",zoom);
 attributes.transform3d = catransform3dmakescale(1.0, zoom, 1.0);
 //底部显示效果
 attributes.frame = cgrectmake(attributes.frame.origin.x, attributes.frame.origin.y + zoom, attributes.size.width, attributes.size.height);
 //居中显示效果
// cgfloat scrolldirectionitemheight = self.itemsize.height;
// cgfloat sideitemfixedoffset = 0;
// sideitemfixedoffset = (scrolldirectionitemheight - scrolldirectionitemheight * 0.7) / 2;
// attributes.center = cgpointmake(attributes.center.x, attributes.center.y + zoom);
 
 }
 return array;
}
 
////设置放大动画
//-(nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect
//{
// nsarray *arr = [self getcopyofattributes:[super layoutattributesforelementsinrect:rect]];
// //屏幕中线
// cgfloat centerx = self.collectionview.contentoffset.x + self.collectionview.bounds.size.width/2.0f;
// //刷新cell缩放
// for (uicollectionviewlayoutattributes *attributes in arr) {
// cgfloat distance = fabs(attributes.center.x - centerx);
// //移动的距离和屏幕宽度的的比例
// cgfloat apartscale = distance/self.collectionview.bounds.size.width;
// //把卡片移动范围固定到 -π/4到 +π/4这一个范围内
// cgfloat scale = fabs(cos(apartscale * m_pi/4));
// //设置cell的缩放 按照余弦函数曲线 越居中越趋近于1
// attributes.transform = cgaffinetransformmakescale(1.0, scale);
// }
// return arr;
//}
 
//防止报错 先复制attributes
- (nsarray *)getcopyofattributes:(nsarray *)attributes
{
 nsmutablearray *copyarr = [nsmutablearray new];
 for (uicollectionviewlayoutattributes *attribute in attributes) {
 [copyarr addobject:[attribute copy]];
 }
 return copyarr;
}
 
- (bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds {
 return true;
}
@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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#import <uikit/uikit.h>
#import "collmodel.h"
@interface collectionviewcell : uicollectionviewcell
@property (nonatomic, strong) collmodel * itemmodel;
 
@end
 
 
#import "collectionviewcell.h"
#define screen_rate ([uiscreen mainscreen].bounds.size.width/375.0)
@interface collectionviewcell()
/**
 * 存放所有下载操作的队列
 */
@property (nonatomic, strong) uiimageview *itemicon;
@property (nonatomic, strong) uilabel *itemlabel;
@property (nonatomic, strong) uilabel *pricelabel;
@end
 
@implementation collectionviewcell
@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 = ([uiscreen mainscreen].bounds.size.width / 5.0) * screen_rate;
 _itemicon.frame = cgrectmake(0, 0, self.contentview.frame.size.width, self.contentview.frame.size.height);
 _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];
 }];
}
@end

运行效果

iOS UICollectionView实现卡片效果

github 下载

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

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

延伸 · 阅读

精彩推荐
  • IOSiOS通过逆向理解Block的内存模型

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

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

    Swiftyper12832021-03-03
  • IOSIOS开发之字典转字符串的实例详解

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

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

    苦练内功5832021-04-01
  • IOS关于iOS自适应cell行高的那些事儿

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

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

    daisy6092021-05-17
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

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

    SimpleWorld11022021-01-28
  • IOS解析iOS开发中的FirstResponder第一响应对象

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

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

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

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

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

    windtersharp7642021-05-04
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

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

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

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

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

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

    xiari5772021-06-01