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

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

服务器之家 - 编程语言 - IOS - ios的collection控件的自定义布局实现与设计

ios的collection控件的自定义布局实现与设计

2021-04-07 16:35技术小百科 IOS

这篇文章主要介绍了mac、iOS端支持自定义布局的collection控件的实现与设计,需要的朋友学习参考下吧。

collection控件用来实现界面的各种自定义布局,最常用其作为横向、竖向的布局控件。很早之前,系统对于collection的支持并不是很好。所以自己实现了支持自定义布局、自定义cell的collection控件。自定义的collection可以满足所有的产品特殊需求及动态效果,例如在某些特殊情况下可能需要除选中cell之外的其它cell执行布局动画等。在collection的基础之上,我又实现了支持cell拖动、拖离窗体的tabview控件。本文主要介绍自定义collection的设计与实现,后续持续更新多tab的tabview控件。

我有几张阿里云幸运券分享给你,用券购买或者升级阿里云相应产品会有特惠惊喜哦!把想要买的产品的幸运券都领走吧!快下手,马上就要抢光了。

产品中的一些实现效果

ios的collection控件的自定义布局实现与设计

mac旺旺表情面板,实现grid与横向布局

ios的collection控件的自定义布局实现与设计

mac千牛工作台用作横向布局

ios的collection控件的自定义布局实现与设计

ios千牛历史登录页面实现当前选中cell变大并且选中cell总中最中位置校准动效的效果

collection

collection主要包括:继承scrollview的collectionview,数据源协议collectionviewdatasource,事件响应协议collectoinviewdelegate,布局基类collectoinlayout以及展示单元collectioncellview。

模块图如下:

ios的collection控件的自定义布局实现与设计

 collectionview

collection容器包含指实现collectionviewdatasource、collectoinviewdelegate协议的指针以及collectoinlayout成员,同时维护collectoincellview的控件重用。

?
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
@interface wwcollectionview : nsscrollview
// 布局对象
@property (retain) wwcollectionviewlayout *layout;
// 数据源
@property (weak) id datasource;
// 事件响应
@property (weak) id delegate;
// 重加载数据
(void)reloaddata;
// 重排布
(void)invalidatelayout;
// 取消返回选中
(void)unselectedall;
// 注册重用对象
(void)registerclass:(class)cellclass forcellwithreuseidentifier:(nsstring *)identifier;
// 对象重用
(id)dequeuereusablecellwithreuseidentifier:(nsstring )identifier forindexpath:(nsindexpath )indexpath;
// 设置选中对象
(void)selectitematindexpath:(nsindexpath *)indexpath animated:(bool)animated;
// 当前选中对象
(nsindexpath *)selecteditem;
// 重加载indexpath item
(void)reloaditemsatindexpath:(nsindexpath *)indexpath;
// 插入
(void)insertitemsatindexpath:(nsindexpath *)indexpath withanimate:(bool)animate;
// 删除
(void)deleteitemsatindexpath:(nsindexpath *)indexpath withanimate:(bool)animate;
// 重新排列
(void)relayoutwithanimation:(bool)animated completion:(void (^)(bool finished))completion;
// 滚动到apoint
(void)scrolltopoint:(nspoint)apoint withanimate:(bool)animate;
@end

collectionviewdatasource

collection展示的数据源,由宿主实现。

?
1
2
3
4
5
6
7
8
@protocol wwcollectionviewdatasource
// 返回indexpath下标的cell
(wwcollectioncellview )collectview:(wwcollectionview )collectionview cellforitematindexpath:(nsindexpath *)indexpath;
// 总cell个数
(nsinteger)numberofitemincollectionview:(wwcollectionview *)collectionview;
// cell的数据
(id)collectionview:(wwcollectionview )colletionview objectvalueatindexpath:(nsindexpath )indexpath;
@end

collectoinviewdelegate

collection事件的回调响应,由宿主实现。

?
1
2
3
4
5
6
@protocol wwcollectionviewdelegate
// indexpath元素被选中
(void)collectionview:(wwcollectionview )collectionview didselectitematindexpath:(nsindexpath )indexpath;
// 是否支持选中
(bool)collectionview:(wwcollectionview )collectionview shouldselectitemsatindexpaths:(nsindexpath )indexpath;
@end

collectoinlayout

collectioncellview的布局方案。

?
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
@interface wwcollectionviewlayout : nsobject
// 布局基类
@property (weak) wwcollectionview *collectionview;
// 每个cell元素大小
@property (assign) nssize itemsize;
// edgeinsets
@property (assign) nsedgeinsets edgeinsets;
// scrollview使用,表示整个画布大小
@property (assign) nssize viewcontentsize;
(instancetype)initwithcollectionview:(wwcollectionview *)collectionview;
(void)invalidatelayout;
// 返回index的cell大小
(nsrect)frameforindexpath:(nsindexpath *)index total:(nsinteger)total;
(nssize)collectionviewcontentsize;
@end
// 横向布局控件
@interface wwflowcollectionviewlayout : wwcollectionviewlayout
@property (assign) cgfloat headmargin;
@property (assign) cgfloat tailmargin;
@end
// grid布局控件
@interface wwgridcollectionviewlayout : wwcollectionviewlayout
// 每行多少个
@property (assign) nsinteger numberperrow;
@property (assign) cgfloat headmargin;
@property (assign) cgfloat tailmargin;
@end
?
1
@implementation wwflowcollectionviewlayout
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(void)invalidatelayout {
nsinteger cellcount = [self.collectionview.datasource numberofitemincollectionview:self.collectionview];
cgrect bounds = self.collectionview.bounds;
// 画布宽度
cgfloat width = _headmargin + _tailmargin + (cellcount - 1) (self.edgeinsets.left + self.edgeinsets.right) + self.itemsize.width cellcount;
if (width < bounds.size.width) {
width = bounds.size.width;
}
self.viewcontentsize = nsmakesize(width, bounds.size.height);
[super invalidatelayout];
}
(nsrect)frameforindexpath:(nsindexpath *)index total:(nsinteger)total {
cgfloat leftpos = self.headmargin + [index indexatposition:0] * (self.itemsize.width + self.edgeinsets.left + self.edgeinsets.right);
// 返回cell的rect
return nsmakerect(leftpos, self.edgeinsets.top, self.itemsize.width, self.itemsize.height);
}
@end

collectoincellview

collection展示的cell控件。

?
1
2
3
4
5
6
7
8
@interface wwcollectioncellview : nsview
// 当前cell被选中
@property (nonatomic, assign) bool selected;
// 数据
@property (nonatomic, retain) id datavalue;
// 使用前重置展示效果
(void)reset;
@end

原文链接:http://blog.csdn.net/qq_36510261/article/details/78741030

延伸 · 阅读

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

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

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

    一片枫叶4662020-12-25
  • IOS关于iOS自适应cell行高的那些事儿

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

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

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

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

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

    xiari5772021-06-01
  • IOSiOS布局渲染之UIView方法的调用时机详解

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

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

    windtersharp7642021-05-04
  • IOSiOS通过逆向理解Block的内存模型

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

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

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

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

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

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

    iOS 雷达效果实例详解

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

    SimpleWorld11022021-01-28
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

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

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

    J_Kang3862021-04-22