姬長信(Redy)

详细分享UICollectionView的自定义布局(瀑布流, 线性, 圆形…)

本文授权转载,作者:ZeroJ(Github

前言

本篇文章不是分享collectionView的详细使用教程, 而是属于比较'高级'的collectionView使用技巧, 阅读之前, 我想你已经很熟悉collectionView的基本使用, 如果不是很熟悉, 建议在以后熟悉一下。那么在本篇结束后, 你也能够很轻松的使用collectionView来实现, 当下比较流行和比较炫酷的效果以及你想要自己实现的其他的效果。这里就实现三种比较常用的效果: 线性布局, 瀑布流布局, 圆形布局, 其他的各种自定义的布局你将是会有能力自己实现的(Demo地址)。

最终效果

一、首先了解下UICollectionViewLayoutAttributes

public var frame: CGRect
public var center: CGPoint
public var size: CGSize
// 用于实现一些3D效果
public var transform3D: CATransform3D
@available(iOS 7.0, *)
public var bounds: CGRect
@available(iOS 7.0, *)
// 更改transform可以方便的实现一些缩放, 旋转效果
public var transform: CGAffineTransform
public var alpha: CGFloat
public var zIndex: Int // default is 0
// 初始化方法, 分别对应为collectionView里面的几种reusebleView
//cell
public convenience init(forCellWithIndexPath indexPath: NSIndexPath)
//SupplementaryView
public convenience init(forSupplementaryViewOfKind elementKind: String, withIndexPath indexPath: NSIndexPath)
// DecorationView
public convenience init(forDecorationViewOfKind decorationViewKind: String, withIndexPath indexPath: NSIndexPath)

二、了解UICollectionViewFlowLayout

要完成collectionView的布局是需要设置他的属性 collectionViewLayout, 当使用storyboard的时候是默认为UICollectionViewFlowLayout。

UICollectionViewFlowLayout是系统提供的一种网格布局, 通常情况下你只需要设置一下下面列举的一些属性, 就可以达到普通的collectionView的使用效果---网格效果, 同时你可以使用UICollectionViewDelegateFlowLayout 来实现一些比较简单的动态更改cell的布局的效果。

// 最小的行距
public var minimumLineSpacing: CGFloat
// 最小的列距
public var minimumInteritemSpacing: CGFloat
// cell的大小
public var itemSize: CGSize
// collectionView的滚动方向
public var scrollDirection: UICollectionViewScrollDirection // default is UICollectionViewScrollDirectionVertical
// headersize
public var headerReferenceSize: CGSize
public var footerReferenceSize: CGSize
public var sectionInset: UIEdgeInsets

三、强大的UICollectionViewLayout

下面是自定义UICollectionViewLayout时比较常用到的一些方法:

func prepareLayout()
func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]?

* 当返回值为true的时候会将collectionView的layout设置为invalidated,将会使collectionView重新调用上面的prepareLayout()...方法重新获得布局

* 同时, 当屏幕旋转的时候collectionView的bounds也会调用这个方法,

如果设置为false, 那么将不会达到屏幕适配的效果,

* 需要注意的是, 当collectionView执行一些操作(delete insert reload)等的时候,

不会调用这个方法, 会直接重新调用上面的prepareLayout()...方法重新获得布局

public func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool
public func collectionViewContentSize() -> CGSize

自定义cell布局的时候重写

public func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?

自定义SupplementaryView的时候重写

public func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?

自定义DecorationView的时候重写

public func layoutAttributesForDecorationViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes?
public func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint

同时里面还有很多的方法我们可以重写来实现更多的效果, 但是这里, 就先介绍这么多的方法来实现自定义collectionView的布局。

下面通过例子介绍下具体的使用

圆形布局的实现

继承自UICollectionViewLayout, 重写prepareLayout(),在这里面我们计算好所有的cell布局

override func prepareLayout() {
    // 一定要调用super
      super.prepareLayout()
      // 初始化需要的数据
      // 总的cell
      totalNum = collectionView!.numberOfItemsInSection(0)
      // 每次计算前需要清零
      layoutAttributes = []
      // 圆心
      center = CGPoint(x: Double(collectionView!.bounds.width * 0.5), y: Double(collectionView!.bounds.height * 0.5))
      // 圆半径
      radius = min(collectionView!.bounds.width, collectionView!.bounds.height) / 3.0
      var indexPath: NSIndexPath
      for index in 0..

重写方法设置每个cell的布局

// Apple建议要重写这个方法, 因为某些情况下(delete insert...)系统可能需要调用这个方法来布局
  override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
      let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
      // 设置cell的大小
      attributes.size = CGSize(width: 60.0, height: 60.0)
      // 当前cell的角度
      // 注意类型转换
      let angle = 2 * CGFloat(M_PI) * CGFloat(indexPath.row) / CGFloat(totalNum)
      // 一点点数学转换
      attributes.center = CGPoint(x: center.x + radius*cos(angle), y: center.y + radius*sin(angle))
      return attributes
  }

数学计算过程(写得不好看##<<>>##)

重写方法返回计算好的布局

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? 
{
    return layoutAttributes
}

重写方法设置collectionView的滚动范围(这里不滚动)

override func collectionViewContentSize() -> CGSize {
      return collectionView!.bounds.size
}

这样就实现了自定义的圆形布局, 还是比较简单!!

关于瀑布流的布局, 自定义过程和这个相似, 就不贴代码了, Demo地址里面有详细的代码注释, 直接大家看代码吧, 如果对你有帮助, 欢迎关注, 欢迎star。

另外Demo里的布局大家是可以直接拿来使用的,以后你也有能力自己实现各种炫酷的布局效果了。

退出移动版