1. UICollectionView
1.Cells
2.Supplementary Views 追加視圖 (類似Header或者Footer)
3.Decoration Views 裝飾視圖 (用作背景展示)
1.1三個代理 :
1.DataSource 提供數(shù)據(jù)
2.Delegate 用戶交互
3.CollectionViewLayout 布局
2. UICollectionViewLayout
2.1 UICollectionViewLayoutAttributes類的介紹:
1一個cell對應(yīng)一個UICollectionViewLayoutAttributes對象
2.UICollectionViewLayoutAttributes對象決定了cell的擺設(shè)位置(frame)
@property (nonatomic) CGRect frame
@property (nonatomic) CGPoint center @property (nonatomic) CGSize size
@property (nonatomic) CATransform3D transform3D
@property (nonatomic) CGFloat alpha
@property (nonatomic) NSInteger zIndex @property (nonatomic, getter=isHidden) BOOL hidden
2.2 自定義的UICollectionViewLayout
需要重載在以下方法:
- (void)prepareLayout 準(zhǔn)備方法被自動調(diào)用,以保證layout實例的正確。
- (CGSize)collectionViewContentSize 返回collectionView的內(nèi)容的尺寸
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 1.返回rect中的所有的元素的布局屬性 2.返回的是包含UICollectionViewLayoutAttributes的NSArray 3.UICollectionViewLayoutAttributes可以是cell,追加視圖或裝飾視圖的信息,通過不同的UICollectionViewLayoutAttributes初始化方法可以得到不同類型的UICollectionViewLayoutAttributes: 1)layoutAttributesForCellWithIndexPath: 2)layoutAttributesForSupplementaryViewOfKind:withIndexPath: 3)layoutAttributesForDecorationViewOfKind:withIndexPath:
-(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath 返回對應(yīng)于indexPath的位置的cell的布局屬性
- (UICollectionViewLayoutAttributes )layoutAttributesForSupplementaryViewOfKind:(NSString )kind atIndexPath:(NSIndexPath *)indexPath 返回對應(yīng)于indexPath的位置的追加視圖的布局屬性,如果沒有追加視圖可不重載
- (UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString)decorationViewKind atIndexPath:(NSIndexPath )indexPath 返回對應(yīng)于indexPath的位置的裝飾視圖的布局屬性,如果沒有裝飾視圖可不重載
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds 當(dāng)邊界發(fā)生改變時,是否應(yīng)該刷新布局。如果YES則在邊界變化(一般是scroll到其他地方)時,將重新計算需要的布局信息。
2.3 調(diào)用順序
1)- (void)prepareLayout 設(shè)置layout的結(jié)構(gòu)和初始需要的參數(shù)等。
2) - (CGSize) collectionViewContentSize 確定collectionView的所有內(nèi)容的尺寸。
3)- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect初始的layout的外觀將由該方法返回的UICollectionViewLayoutAttributes來決定。4)在需要更新layout時,需要給當(dāng)前l(fā)ayout發(fā)送 1)-invalidateLayout, 該消息會立即返回,并且預(yù)約在下一個loop的時候刷新當(dāng)前l(fā)ayout 2)-prepareLayout, 3)依次再調(diào)用-collectionViewContentSize和-layoutAttributesForElementsInRect來生成更新后的布局。
繼承UICollectionViewLayout之后只需要重載幾個提供布局核心特性的方法,其他方法只需按情況重載即可,核心特性如下:
1 指定可滾動內(nèi)容區(qū)域的size
2 為布局中的每個Cell及view提供屬性對象