UICollectionViewDataSource
,UICollectionViewDelegate
,UICollectionViewDelegateFlowLayout
这三个协议。// 行间距,也能够经过collectionView: layout:minimumLineSpacingForSectionAtIndex: @property (nonatomic) CGFloat minimumLineSpacing; // 设置cell之间的间距 @property (nonatomic) CGFloat minimumInteritemSpacing; // 定义每个item的大小。经过设定itemSize能够全局地改变全部cell的尺寸,若是想要对某个cell制定尺寸, //可使用-collectionView:layout:sizeForItemAtIndexPath:方法。 @property (nonatomic) CGSize itemSize; @property (nonatomic) CGSize estimatedItemSize // 滚动方向,默认是水平 // UICollectionViewScrollDirectionVertical // UICollectionViewScrollDirectionHorizontal @property (nonatomic) UICollectionViewScrollDirection scrollDirection; // 根据滚动方向不一样,header和footer的高和宽中只有一个会起做用。 // 垂直滚动时section间宽度为该尺寸的高,而水平滚动时为宽度起做用, @property (nonatomic) CGSize headerReferenceSize; @property (nonatomic) CGSize footerReferenceSize; // 组间距 缩进 @property (nonatomic) UIEdgeInsets sectionInset;
// 设定指定Cell的尺寸 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; // 设定collectionView(指定区)的边距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section; // 设定指定区内Cell的最小行距,也能够直接设置UICollectionViewFlowLayout的minimumLineSpacing属性 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section // 设定指定区内Cell的最小间距,也能够直接设置UICollectionViewFlowLayout的minimumInteritemSpacing属性 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
// 边框 @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
// 返回collectionView的内容的尺寸 -(CGSize)collectionViewContentSize // 返回rect中的全部的元素的布局属性 /* 返回的是包含UICollectionViewLayoutAttributes的NSArray UICollectionViewLayoutAttributes能够是cell,追加视图或装饰 视图的信息,经过不一样的UICollectionViewLayoutAttributes初始 化方法能够获得不一样类型的UICollectionViewLayoutAttributes: */ -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect // 返回对应于indexPath的位置的cell的布局属性 -(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath //返回对应于indexPath的位置的追加视图的布局属性,若是没有追加视图可不重载 -(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath // 返回对应于indexPath的位置的装饰视图的布局属性,若是没有装饰视图可不重载 -(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath // 当边界发生改变时,是否应该刷新布局。若是YES则在边界变化(通常是scroll到其余地方)时,将从新计算须要的布局信息。 -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
另外须要了解的是,在初始化一个UICollectionViewLayout实例后,会有一系列准备方法被自动调用,以保证layout实例的正确。布局
// 当指定indexPath处的item被选择时触发 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath //返回这个UICollectionView是否能够被选择 -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath; // 下面是三个和高亮有关的方法: - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath /* 事件的处理顺序以下: 1.手指按下 2.shouldHighlightItemAtIndexPath (若是返回YES则向下执行,不然执行到这里为止) 3.didHighlightItemAtIndexPath (高亮) 4.手指松开 5.didUnhighlightItemAtIndexPath (取消高亮) 6.shouldSelectItemAtIndexPath (若是返回YES则向下执行,不然执行到这里为止) 7.didSelectItemAtIndexPath (执行选择事件) */