您的位置:首页 > 移动开发 > IOS开发

自定义collectionView,最后一行的居中

2015-12-27 11:12 661 查看

collectionView 都是从第一位开始的,想要达到居中的效果就要自定义一个瀑布流.一个小 demo, 和大家分享一下.

在 viewcontroller 里面

#import "ViewController.h"

#import "CustomFlowLayout.h"

#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#define ScreenHeight [UIScreen mainScreen].bounds.size.height

#define Scale [UIScreen mainScreen].bounds.size.width /
320

#define kMinimumLineSpacing 20

#define kMinimumInteritemSpacing 15

#define kItemWidth 45

#define kItemHeight kItemWidth

#define WIDTH self.view.frame.size.width

@interface
ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,
strong) UICollectionView *myCollectionView;

@property (nonatomic,
strong) CustomFlowLayout *flowLayout;

@property (nonatomic,
strong) NSMutableArray *itemArray;

@end

@implementation ViewController

- (void)viewDidLoad

{

    [super
viewDidLoad];

    self.view.backgroundColor = [UIColor
whiteColor];

    _flowLayout = [[CustomFlowLayout
alloc]
init];

    //
设置滚动方向

    _flowLayout.scrollDirection =
UICollectionViewScrollDirectionVertical;

    //
设置item大小

    _flowLayout.itemSize =
CGSizeMake(kItemWidth *
Scale, kItemHeight *
Scale);

    //
设置最小行间距

    _flowLayout.minimumLineSpacing =
kMinimumLineSpacing * Scale;

    //
设置最小列间距

    _flowLayout.minimumInteritemSpacing =
kMinimumInteritemSpacing * Scale;

   
// 设置一个item边框,上下左右边距大小

    _flowLayout.sectionInset =
UIEdgeInsetsMake(kMinimumLineSpacing *
Scale, 16.5 *
Scale, 0,
16.5 *
Scale);

    

    for (int i =
0; i < 9; i++) {

        NSNumber *number = [NSNumber
numberWithInt:i];

        [self.itemArray
addObject:number];

    }

    

    NSInteger lineRow =
0;

    NSInteger remainder =
_itemArray.count %
5;

    if (remainder ==
0) {

        lineRow = _itemArray.count /
5;

    }else {

        lineRow = _itemArray.count /
5 + 1;

    }

    NSLog(@"==%ld",_itemArray.count);

    

    _myCollectionView = [[UICollectionView
alloc]
initWithFrame:CGRectMake(0,
0.4625 *
WIDTH, ScreenWidth, ((kMinimumLineSpacing +
kItemHeight) * Scale) * lineRow +
kMinimumLineSpacing) collectionViewLayout:_flowLayout];

    _myCollectionView.dataSource =
self;

    _myCollectionView.delegate =
self;

    _myCollectionView.backgroundColor = [UIColor
cyanColor];

    [_myCollectionView
registerClass:[UICollectionViewCell
class] forCellWithReuseIdentifier:@"FightGroupsDetailCollectionCellReuse"];

    [self.view
addSubview:_myCollectionView];

}

- (NSMutableArray *)itemArray

{

    if (!_itemArray) {

        _itemArray = [NSMutableArray
array];

    }

    return
_itemArray;

}

#pragma mark - collectionView的协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return
_itemArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:@"FightGroupsDetailCollectionCellReuse"
forIndexPath:indexPath];

    cell.backgroundColor = [UIColor
redColor];

    return cell;

}

在 controller 里面写好以后我们要自定义 CustomFlowLayout 了.CustomFlowLayout要继承于UICollectionViewFlowLayout

在CustomFlowLayout.m里面

#import "CustomFlowLayout.h"

#define SCREENW [UIScreen mainScreen].bounds.size.width

#define Scale [UIScreen mainScreen].bounds.size.width /
320

#define kMinimumInteritemSpacing 15

#define itemOneLineCount 5

#define kItemWidth 45

@implementation CustomFlowLayout

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

{

    NSArray* attributesToReturn = [super
layoutAttributesForElementsInRect:rect];

    for (UICollectionViewLayoutAttributes* attributes
in attributesToReturn) {

        if (nil == attributes.representedElementKind) {

            NSIndexPath* indexPath = attributes.indexPath;

            attributes.frame = [self
layoutAttributesForItemAtIndexPath:indexPath].frame;

        }

    }

    return attributesToReturn;

}

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

{

    UICollectionViewLayoutAttributes *currentItemAttributes = [super
layoutAttributesForItemAtIndexPath:indexPath];

    NSInteger sectionCount = [self.collectionView
numberOfSections];

    NSInteger itemCount =
0;

    for (int i =
0; i < sectionCount; i++) {

        NSInteger rowCount = [self.collectionView
numberOfItemsInSection:i];

        itemCount = itemCount + rowCount;

    }

    //
余数

    NSInteger yushu = itemCount %
itemOneLineCount;

    //
行数

    NSInteger hangshu = itemCount /
itemOneLineCount;

    

    if (indexPath.item > hangshu *
itemOneLineCount -
1) {

        NSInteger index = indexPath.item - (hangshu *
itemOneLineCount -
1);

        switch (yushu) {

            case 1: {

                // 最后一行只有一个时候

                //
由于作用域的问题 必须{}扩起来

                CGPoint currentItemCenter = [currentItemAttributes
center];

                currentItemCenter.x =
SCREENW / 2;

                currentItemAttributes.center = currentItemCenter;

            }

                break;

            case 2: {

                // 最后一行只有两个时候

                CGRect currentItemFrame = [currentItemAttributes
frame];

                if (index ==
1) {

                    // 最后一行第一个

                    // (屏幕宽度
-(两个itemsize的宽度

中间间隔)) / 2

                    currentItemFrame.origin.x =
SCREENW / 2 -
kMinimumInteritemSpacing *
Scale / 2 -
kItemWidth * Scale;

                } else
if (index == 2) {

                    // (屏幕宽度

(两个itemsize的宽度

中间间隔)) / 2 +
中间间隔 + itemsize宽度

                    currentItemFrame.origin.x =
SCREENW / 2 +
kMinimumInteritemSpacing *
Scale / 2;

                }

                currentItemAttributes.frame = currentItemFrame;

            }

                break;

            case 3: {

                CGRect currentItemFrame = [currentItemAttributes
frame];

                if (index ==
1) {

                    currentItemFrame.origin.x =
SCREENW / 2 -
kMinimumInteritemSpacing *
Scale - kItemWidth *
Scale * 1.5;

                } else
if (index == 2) {

                    currentItemFrame.origin.x =
SCREENW / 2 -
kItemWidth * Scale /
2;

                } else
if (index == 3) {

                    currentItemFrame.origin.x =
SCREENW / 2 +
kItemWidth * Scale /
2 + kMinimumInteritemSpacing *
Scale;

                }

                currentItemAttributes.frame = currentItemFrame;

            }

                break;

            case 4: {

                CGRect currentItemFrame = [currentItemAttributes
frame];

                if (index ==
1) {

                    currentItemFrame.origin.x =
SCREENW / 2 -
kMinimumInteritemSpacing *
Scale * 1.5 -
kItemWidth * Scale *
2;

                } else
if (index == 2) {

                    currentItemFrame.origin.x =
SCREENW / 2 -
kMinimumInteritemSpacing /
2 - kItemWidth *
Scale;

                } else
if (index == 3) {

                    currentItemFrame.origin.x =
SCREENW / 2 +
kMinimumInteritemSpacing *
Scale / 2;

                } else
if (index == 4) {

                    currentItemFrame.origin.x =
SCREENW / 2 +
kItemWidth * Scale +
kMinimumInteritemSpacing *
Scale * 1.5;

                }

                currentItemAttributes.frame = currentItemFrame;

            }

                break;

            default:

                break;

        }

    }

    return currentItemAttributes;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息