UICollectionView的简单布局
来源:互联网 发布:js涂料 编辑:程序博客网 时间:2024/05/22 11:44
UICollectionView的简单布局
如果使用的是UICollectionViewController的话,就重写init方法
// 重写init方法- (instancetype)init { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; return [self initWithCollectionViewLayout:flowLayout];}
初始化的时候请注意用的类,初始化用的是UICollectionViewFlowLayout类而不是UICollectionViewLayout;
接着就是注册要使用的cell、Header、footer
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerID];接下来的是代理方法
#pragma mark - UICollectionViewDelegateFlowLayout// 个人认为:cell的Size- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake((320-20)/3, 150);}/* 个人认为:同section中纵向的两个Item的间距 */- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 5;}/* 个人认为:同section中横向的两个Item的间距 */- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 5;}// 个人认为:collectionView的边距 -- 上、左、下、右- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(30, 5, 5, 5);// 上.左.下.右}// sectionHeader、 sectionFooter- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *headerView; if (indexPath.section == 0) { headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath]; if (headerView == nil) { headerView = [[UICollectionReusableView alloc] init]; } headerView.backgroundColor = [UIColor redColor]; [headerView addSubview:self.rotate]; } return headerView; // if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { // UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerID forIndexPath:indexPath]; // if (headerView == nil) { // headerView = [[UICollectionReusableView alloc] init]; // } // headerView.backgroundColor = [UIColor redColor]; // [headerView addSubview:self.rotate]; // return headerView; // } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) { // UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerID forIndexPath:indexPath]; // if (footerView == nil) { // footerView = [[UICollectionReusableView alloc] init]; // } // footerView.backgroundColor = [UIColor grayColor]; // return footerView; // } // return nil;}// sectionFooter的Size//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {// return CGSizeMake(DMScreenWidth, 11);//}// sectionHeader的Size- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { CGSize size = CGSizeZero; if (section == 0) { size = CGSizeMake(DMScreenWidth, 100); } return size;}#pragma mark - UICollectionViewDelegate、UICollectionViewDataSource- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 6;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DMNormalViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; cell.backgroundColor = [UIColor yellowColor]; cell.data = nil; NSLog(@"%@",cell); return cell;}// 旋转- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{ CATransform3D rotation;//3D旋转 // rotation = CATransform3DMakeTranslation(0 ,50 ,20); rotation = CATransform3DMakeRotation( M_PI_4 , 0.0, 0.7, 0.4); //逆时针旋转 rotation = CATransform3DScale(rotation, 0.8, 0.8, 1); rotation.m34 = 1.0/ 1000; cell.layer.shadowColor = [[UIColor redColor]CGColor]; cell.layer.shadowOffset = CGSizeMake(10, 10); cell.alpha = 0; cell.layer.transform = rotation; [UIView beginAnimations:@"rotation" context:NULL]; //旋转时间 [UIView setAnimationDuration:0.6]; cell.layer.transform = CATransform3DIdentity; cell.alpha = 1; cell.layer.shadowOffset = CGSizeMake(0, 0); [UIView commitAnimations]; }
阅读全文
0 0
- UICollectionView的简单布局
- 自定义UICollectionView的布局
- UICollectionView的布局
- UICollectionView 的布局
- UICollectionView的自定义布局
- UICollectionView的简单使用
- UICollectionView的简单创建
- UICollectionView的简单使用
- UICollectionView的简单使用
- UICollectionView的简单使用
- UICollectionView的简单使用
- UICollectionView的简单介绍
- UICollectionView的简单介绍
- UICollectionVIew的简单使用
- UICollectionView的简单介绍
- UICollectionView的简单使用
- UICollectionView的简单使用
- UICollectionView的多边形布局-蜂窝布局
- 【故障】旧版本的Docker Toolbox 无法正常运行的解决办法
- Kotlin学习过程 一 Kotlin插件安装和第一个Kotlin app
- Kotlin从入门到“放弃”(一)
- 机器学习相关模块笔记
- iframe是什么意思?是文档中的文档,即一个html文档中,包含另一个html文档
- UICollectionView的简单布局
- Linux--shel正则表达式
- <div id="container"> <div id="header"> <div class="header"> <div id="blog_title"
- 文字无缝滚动
- 分治算法经典例题 寻找假币
- Android应用签名
- 使用阻塞队列爬取代理ip实现爬虫
- 程序员之路
- Solr-6.5.1 配置schema.xml