当前位置: 移动技术网 > IT编程>移动开发>IOS > 自定义流水布局(UICollectionViewFlowLayout的基本使用)

自定义流水布局(UICollectionViewFlowLayout的基本使用)

2018年12月27日  | 移动技术网IT编程  | 我要评论

新著龙虎门在线漫画,孙天柏,揭阳男科8643333

最终显示的效果图

思路:

1、uicollection的基本设置,并且创建一个继承自uicollectionviewflowlayout的类。(不能是uicollectionviewlayout,否则全部都需要自定义)

2、在uicollectionviewflowlayout类中完成四步

  - 1)重写preparelayout方法进行基本的布局(cell在最左面的时候是在正中间),不能在init中布局,因为设置collectionview尺寸是在viewdidload中,而init在它之前调用,获得的collectionview的尺寸是空的

  - 2)重写shouldinvalidatelayoutforboundschange,当collectionview的显示范围发生改变的时候,让其内部重新布局(即让cell滚动起来)

  - 3)重写layoutattributesforelementsinrect方法,让cell在左右滑动的时候,尺寸放大或缩小

  - 4)重写targetcontentoffsetforproposedcontentoffset方法,让最接近中心的cell在停在正中央。

 

代码如下:

viewcontorller中:

 1 #import "viewcontroller.h"
 2 #import "zwlinelayout.h"
 3 @interface viewcontroller () 
 4 @end
 5 @implementation viewcontroller
 6 static  nsstring *zwcellid = @"cell";
 7 - (void)viewdidload {
 8     [super viewdidload];
 9     //若为uicollectionviewlayout,itemsize和scrolldirection都需要自己写,下面的类继承自uicollectionviewlayout
10     zwlinelayout *layout = [[zwlinelayout alloc] init];
11     layout.itemsize = cgsizemake(160, 160);
12     cgrect rect = cgrectmake(0, 100, self.view.frame.size.width, self.view.frame.size.width * 0.6);
13     uicollectionview *collection = [[uicollectionview alloc] initwithframe:rect collectionviewlayout:layout];
14     collection.datasource = self;
15     collection.backgroundcolor = [uicolor greencolor];
16     [self.view addsubview:collection];
17     [collection registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:zwcellid];
18 }
19 
20 #pragma mark - 数据源方法
21 - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section
22 {
23     return 10;
24 }
25 - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath
26 {
27     uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:zwcellid forindexpath:indexpath];
28     cell.backgroundcolor = [uicolor orangecolor];
29     return cell;
30 }

zwlinelayout.m中

 1 #import "zwlinelayout.h"
 2 
 3 @implementation zwlinelayout
 4 
 5 /**
 6  * 用来做布局的初始化操作(不建议在init方法中进行布局的初始化操作)
 7  */
 8 - (void)preparelayout
 9 {
10     [super preparelayout];
11     //水平滚动
12     self.scrolldirection = uicollectionviewscrolldirectionhorizontal;
13     
14     //
15     cgfloat margin = (self.collectionview.frame.size.width - self.itemsize.width) / 2;
16     self.collectionview.contentinset = uiedgeinsetsmake(0, margin, 0, margin);
17 }
18 
19 /**
20  * 当collectionview的显示范围发生改变的时候,是否需要重新刷新布局
21  * 一旦重新刷新布局,就会重新调用下面的方法:
22  * 1.preparelayout
23  * 2.layoutattributesforelementsinrect:方法
24  */
25 - (bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds
26 {
27     return yes;
28 }
29 
30 
31 /**
32  * 这个方法的返回值是一个数组(数组里面存放着rect范围内所有元素的布局属性)
33  * 这个方法的返回值决定了rect范围内所有元素的排布(frame)
34  */
35 //需要在viewcontroller中使用上zwlinelayout这个类后才能重写这个方法!!
36 - (nsarray *)layoutattributesforelementsinrect:(cgrect)rect
37 {
38     //让父类布局好样式
39     nsarray *arr = [super layoutattributesforelementsinrect:rect];
40     //计算出collectionview的中心的位置
41     cgfloat ceterx = self.collectionview.contentoffset.x + self.collectionview.frame.size.width * 0.5;
42     /**
43      * 1.一个cell对应一个uicollectionviewlayoutattributes对象
44      * 2.uicollectionviewlayoutattributes对象决定了cell的frame
45      */
46     for (uicollectionviewlayoutattributes *attributes in arr) {
47         //cell的中心点距离collectionview的中心点的距离,注意abs()表示绝对值
48         cgfloat delta = abs(attributes.center.x - ceterx);
49         //设置缩放比例
50         cgfloat scale = 1.1 - delta / self.collectionview.frame.size.width;
51         //设置cell滚动时候缩放的比例
52         attributes.transform = cgaffinetransformmakescale(scale, scale);
53     }
54     
55     return arr;
56 }
57 
58 /**
59  * 这个方法的返回值,就决定了collectionview停止滚动时的偏移量
60  */
61 - (cgpoint)targetcontentoffsetforproposedcontentoffset:(cgpoint)proposedcontentoffset withscrollingvelocity:(cgpoint)velocity
62 {
63     // 计算出最终显示的矩形框
64     cgrect rect;
65     rect.origin.y = 0;
66     rect.origin.x = proposedcontentoffset.x;
67     rect.size = self.collectionview.frame.size;
68     
69     //获得super已经计算好的布局的属性
70     nsarray *arr = [super layoutattributesforelementsinrect:rect];
71     
72     //计算collectionview最中心点的x值
73     cgfloat centerx = proposedcontentoffset.x + self.collectionview.frame.size.width * 0.5;
74     
75     cgfloat mindelta = maxfloat;
76     for (uicollectionviewlayoutattributes *attrs in arr) {
77         if (abs(mindelta) > abs(attrs.center.x - centerx)) {
78             mindelta = attrs.center.x - centerx;
79         }
80     }
81     proposedcontentoffset.x += mindelta;
82     return proposedcontentoffset;
83 }
84 @end

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网