iOS开发好文iOS开发首页投稿(暂停使用,暂停投稿)

使用UICollectionView自定义样式01-水平线性布局

2016-07-21  本文已影响503人  aLonelyRoot3

UICollectionView 是 iOS6 推出的 API, 可以实现复杂的定制效果, 使用和 UITableView 类似, 这篇主要通过自定义 UICollectionViewFlowLayout 实现一个左右滑动, 支持放大的相册效果

效果展示

003.png

思路

API 简介

UICollectionViewLayout 提供了以下 API 控制自定义布局的样式, 实现定制效果

01 - layoutAttributesForElementsInRect:

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect

02 - prepareLayout

- (void)prepareLayout;

03 - shouldInvalidateLayoutForBoundsChange:

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds; // return YES to cause the collection view to requery the layout for geometry information

如果返回YES,那么collectionView显示的范围发生改变时,就会重新刷新布局
一旦重新刷新布局,就会按顺序调用下面的方法:

04 - targetContentOffsetForProposedContentOffset:

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity; // return a point at which to rest after scrolling - for layouts that want snap-to-point scrolling behavior

这个方法的返回值,就决定了collectionView停止滚动时的偏移量
参数:

具体实现

01 - 重写 layoutAttributesForElementsInRect: 方法]

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
 // 获得super已经计算好的布局属性
 NSArray *array = [super layoutAttributesForElementsInRect:rect];

 // 计算collectionView最中心点的x值
 CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;

 // 在原有布局属性的基础上,进行微调
 for (UICollectionViewLayoutAttributes *attrs in array) {
   // cell的中心点x 和 collectionView最中心点的x值 的间距
   CGFloat delta = ABS(attrs.center.x - centerX);
  
   // 根据间距值 计算 cell的缩放比例
   CGFloat scale = 1 - delta / self.collectionView.frame.size.width;
  
   // 设置缩放比例
   attrs.transform = CGAffineTransformMakeScale(scale, scale);
 }

 return array;
}

02 - 重写 shouldInvalidateLayoutForBoundsChange 方法, 返回 YES

意味着当collectionView显示的范围发生改变时,就会重新刷新布局, 一旦刷新布局, 就会按顺序调用:

- (void)prepareLayout;
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect

03 - 重写 targetContentOffsetForProposedContentOffset: 方法决定 collectionView 停止滚动时的偏移量

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
  // 计算出最终显示的矩形框
  CGRect rect;
  rect.origin.y = 0;
  rect.origin.x = proposedContentOffset.x;
  rect.size = self.collectionView.frame.size;
 
  // 获得super已经计算好的布局属性
  NSArray *array = [super layoutAttributesForElementsInRect:rect];
 
  // 计算collectionView最中心点的x值
  CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;
 
  // 存放最小的间距值
  CGFloat minDelta = MAXFLOAT;
  for (UICollectionViewLayoutAttributes *attrs in array) {
    if (ABS(minDelta) > ABS(attrs.center.x - centerX)) {
      minDelta = attrs.center.x - centerX;
    }
  }
 
  // 修改原有的偏移量
  proposedContentOffset.x += minDelta;
  return proposedContentOffset;
}
    ```

#### 04 - (补充) 关于 scale - 缩放比例
 计算出 cell 的中心线和 collectionView 的 contentView 的中心线的绝对距离 delta
- 计算collectionView最中心点的x值
    ```
    CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
    ```
- 计算cell的中心点 x 和 collectionView 最中心点的 x 值的间距

CGFloat delta = ABS(attrs.center.x - centerX);
```
CGFloat scale = 1 - delta / self.collectionView.frame.size.width;

UICollectionView 的优点

总结

程序代码地址

https://git.oschina.net/aLonelyRoot3/AYCustomLayout.git
UICollectionView 实现的很多其他流行效果会在以后贴出

欢迎关注作者, 点个喜欢,如有闲钱,欢迎打赏。

上一篇 下一篇

猜你喜欢

热点阅读