常见的ui展示形式tab、collection等iOS学习笔记iOS Developer

继承UICollectionViewLayout 自定义布局展示

2016-09-06  本文已影响382人  阿拉斯加的狗

重写prepareLayout方法

重写layoutAttributesForElementsInRect:方法

重写shouldInvalidateLayoutForBoundsChange:方法

重写targetContentOffsetForProposedContentOffset:withScrollingVelocity:方法

示例展示

自定义布局.gif

/**
 *   准备布局
 */
- (void)prepareLayout {

    [super prepareLayout];
    
    self.itemSize = CGSizeMake(200, 200);
    
    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    //设置起始的内边距
    CGFloat inset = (self.collectionView.frame.size.width - self.itemSize.width) * 0.5;
    self.sectionInset = UIEdgeInsetsMake(0, inset, 0, inset);
    
}

/**
 *   用来刷新重新布局
 */
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {

    return YES;

}

/**
 *   用来布局cell的frame
 */

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {

    NSArray *attrsArray = [super layoutAttributesForElementsInRect:rect];
    
    //获取到collectionView的中心点的X
    CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;
    
    for (UICollectionViewLayoutAttributes *attr in attrsArray) {
        
        CGFloat delta = ABS(attr.center.x - centerX);
        
        CGFloat scale = 1 - delta / self.collectionView.frame.size.width;
        
        attr.transform = CGAffineTransformMakeScale(scale, scale);
    }

    return attrsArray;
}

/**
 *   用来布局cell最终的落点
 */
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity {

    CGRect rect = CGRectMake(proposedContentOffset.x, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height);
    
    //获取到cell的frame
    NSArray *attrsArray = [super layoutAttributesForElementsInRect:rect];

    CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;
    
    //计算cellectionView中心点最小的值
     CGFloat minDetla = CGFLOAT_MAX;
    for (UICollectionViewLayoutAttributes *attr in attrsArray) {
     
        if (ABS(minDetla) > ABS(centerX - attr.center.x)) {
            
            minDetla = attr.center.x - centerX;
        }
        
    }
        proposedContentOffset.x += minDetla;
    
    return proposedContentOffset;
    
}

github:https://github.com/aryehToDog/WK-collectionView

上一篇 下一篇

猜你喜欢

热点阅读