话不多说,直接上代码,自定义cell的代码就不贴了,很简单的放了个Label,这样的写法在动态切换时存在一个问题,cell内部的label的动画无法展示,处理办法是在设置isList时,通知cell内部进行动画
//
// ViewController.m
// XACollectionAnimationChangeProject
//
// Created by wj on 2019/9/10.
// Copyright © 2019年 XA. All rights reserved.
//
#import "ViewController.h"
#import "XAGridCollectionViewCell.h"
#define XAWidth [UIScreen mainScreen].bounds.size.width
#define XAHeight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic , retain)UICollectionView *collectionView;
/**
多行多列式布局
*/
@property (nonatomic , retain)UICollectionViewFlowLayout *gridLayout;
/**
多行一列式布局
*/
@property (nonatomic , retain)UICollectionViewFlowLayout *listLayout;
@property (nonatomic , assign)BOOL isList;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.collectionView];
self.collectionView.frame = CGRectMake(0, 0, XAWidth, XAHeight);
}
#pragma mark
- (UICollectionViewFlowLayout *)listLayout {
if (!_listLayout) {
_listLayout = [[UICollectionViewFlowLayout alloc] init];
_listLayout.itemSize = CGSizeMake(XAWidth, 200);
_listLayout.minimumLineSpacing = 5;
_listLayout.sectionInset = UIEdgeInsetsZero;
}
return _listLayout;
}
- (UICollectionViewFlowLayout *)gridLayout {
if (!_gridLayout) {
_gridLayout = [[UICollectionViewFlowLayout alloc] init];
CGFloat itemWidth = (self.view.frame.size.width - 5) * 0.5;
_gridLayout.itemSize = CGSizeMake(itemWidth, 300);
_gridLayout.minimumLineSpacing = 5;
_gridLayout.minimumInteritemSpacing = 5;
_gridLayout.sectionInset = UIEdgeInsetsZero;
}
return _gridLayout;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.gridLayout];
[_collectionView registerClass:[XAGridCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
_collectionView.backgroundColor = [UIColor grayColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
if (@available(iOS 11.0, *)) {
_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
return _collectionView;
}
- (void)setIsList:(BOOL)isList {
_isList = isList;
if (isList) {
[self.collectionView setCollectionViewLayout:self.listLayout animated:YES];
}else{
[self.collectionView setCollectionViewLayout:self.gridLayout animated:YES];
}
[self.collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
self.isList = !self.isList;
}
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
XAGridCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
return cell;
}
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 12;
}
//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return ((UICollectionViewFlowLayout *)collectionViewLayout).itemSize;
}
@end
网友评论