之前一直在使用UITableView,一直想找个机会好好使用一下collectionView。今天自己尝试使用了一下,并且有炫酷效果哦,写篇文章总结一下,希望对想要使用的你有所帮助。
1.首先我们直接来创建一个collectionView对象

这里需要注意两点
a.Frame值 这个可以根据自己的需要设置
b.collectionViewLayout: 这个参数是UICollectionView用来进行展示item布局的 我们最常使用的是流水布局
注: 这里的布局参数类是一个抽象类,不能初始化对象,我们在开发过程中一般使用它的子类UICollectionViewFlowLayout 来设置
//1.1 常用流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
2.我们可以指定代理对象

此时类比UITableView 我们就要遵守代理协议,实现数据源方法

到了这一步看似差不多可以了,不过这里有一个特别注意的点,就是UiCollectionView在使用的时候必须注册cell

这个时候我们就可以运行一下看看效果了。

以上就是简单使用UICollectionView,不过在工作中会有相对复杂的需求,不过这些看似高级的使用 也是在基础之上进行了特殊的设置。我们先把上面的代码放上来,大家可以参照一下。

如果我们需要自己设置每一个cell的展示样式和大小设置 可以通过修改布局参数UICollectionViewFllowLayout的对象参数来设置。

1.要自己写一个布局参数类 继承自流水布局 UICollectionViewFllowLayout
2.当cell的位置相对屏幕中心改变时 重新约束cell
3.相关代码如下:

4.除了上述的计算之外 必须在自定义的约束类中设置一个属性

以上就是炫酷效果的实现了,如果你觉得那里有错误的地方,欢迎指教。邮箱zihanvip520@163.com。
网友评论