iOS 艾欧艾斯iOS开发收集collectionview

关于UICollectionViewLayout的一些理解

2016-08-17  本文已影响2734人  gao_smile
- (void)prepareLayout; 

第一次加载layout、刷新layout、以及

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds;

这个方法 return yes 时,会调用。

实现该方法后应该调用[super prepareLayout]保证初始化正确。用来准备一些布局所需要的信息。
该方法和init方法相似,但前者可能会被调用多次,所以一些不固定的计算(比如该计算和collectionView的尺寸相关),最好放在这里,以保证collectionView发生变化时,自定义CollectionView能做出正确的反应。

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect; 

该方法用来返回rect范围内的 cell supplementary 以及 decoration的布局属性layoutAttributes(这里保存着她们的尺寸,位置,indexPath等等)

- (nullable UICollectionViewLayoutAttributes* )layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath; 

该方法不是必须实现的,即便你实现了,我们对collectionView的任何操作,也不会导致系统主动调用该方法。该方法通常用来定制某个IndexPath的item的属性。

当然我们也可以重写这个方法,将布局时相关的属性设置放在这里,在

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> * )layoutAttributesForElementsInRect:(CGRect)rect 或者 - (void)prepareLayout 

中 需要创建用来返回给系统的属性数组 主动调用这个方法,并添加带可变数组中去返回给系统。当然我们也可以在

- (void)prepareLayout 中 通过[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]

获取 每个indexPath的attributes,在

- (void)prepareLayout

中设置所有item的属性。看需求以及个人喜欢。

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds; 

用来刷新layout的,当我们返回yes的时候。如果我们的需求不需要实时的刷新layout,那么最好判断newBounds 和 我们的collectionView的bounds是否相同,不同时返回yes;(例如苹果官方的lineLayout,因为每次滑动都要放大item,所以这了就直接返回yes)。

以苹果官方的lineLayout为例,这个是对UICollectionViewFlowLayout的扩充,

-(id)init
{
self = [super init];
if (self) {
    self.itemSize = CGSizeMake(ITEM_SIZE, ITEM_SIZE);
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.sectionInset = UIEdgeInsetsMake(200, 0.0, 200, 0.0);//上下边距
    self.minimumLineSpacing = 50.0;//行间距
}
return self;
}

这里初始化一些信息。

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}

因为滑动放大,故这里需要返回yes,实时刷新layout。

-(NSArray* )layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray* array = [super layoutAttributesForElementsInRect:rect];

//可视rect
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;

//设置item的缩放
for (UICollectionViewLayoutAttributes* attributes in array) {
    if (CGRectIntersectsRect(attributes.frame, rect)) {
        CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;//item到中心点的距离
        CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;//距离除以有效距离得到标准化距离
        //距离小于有效距离才生效
        NSLog(@"%f",distance);
        if (ABS(distance) < ACTIVE_DISTANCE) {
            CGFloat zoom = 1 + ZOOM_FACTOR*(1 - ABS(normalizedDistance));//缩放率范围1~1.3,与标准距离负相关
            attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);//x,y轴方向变换
            //attributes.zIndex = 0;
        }
    }
}

return array;
}

这里对item进行放大操作(因为该类是继承自UICollectionViewFlowLayout,苹果重写了该方法,所以调用super可以拿到当前rect范围内attributes,如果继承自UICollectionViewLayout,调用super是什么都拿不到的)。这里的思想就是:距离屏幕中心超过一定距离(假设200,自己设定)开始放大,CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;item中心到屏幕中心点距离占200的比例,

CGFloat zoom = 1 + ZOOM_FACTOR*(1 - ABS(normalizedDistance));
这样便得到放大的倍数。

- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
//proposedContentOffset是没有对齐到网格时本来应该停下的位置

//计算出实际中心位置
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);

//取当前屏幕中的UICollectionViewLayoutAttributes
CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
NSArray* array = [super layoutAttributesForElementsInRect:targetRect];

//对当前屏幕中的UICollectionViewLayoutAttributes逐个与屏幕中心进行比较,找出最接近中心的一个
for (UICollectionViewLayoutAttributes* layoutAttributes in array) {
    CGFloat itemHorizontalCenter = layoutAttributes.center.x;
    if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
        offsetAdjustment = itemHorizontalCenter - horizontalCenter;
    }
}

//返回调整好的point
return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}

-----------------------------------------分割线 ------------------------------------------

    - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity   

这个方法简单理解可以当作是用来设置collectionView的偏移量的,计算当前屏幕哪个item中心点距离屏幕中心点近,就将该item拉到中心去。

上一篇 下一篇

猜你喜欢

热点阅读