UICollectionView——iOS学习连载27
来源:互联网 发布:淘宝u站九块九包邮 编辑:程序博客网 时间:2024/06/07 01:03
1.创建布局对象
UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayoutalloc] init];
2.设置滑动方向
flowLayOut.scrollDirection =UICollectionViewScrollDirectionHorizontal;
3.设置最小间距
flowLayOut.minimumInteritemSpacing =0;
flowLayOut.minimumLineSpacing =0;
4.设置单元格的大小
flowLayOut.itemSize = CGSizeMake(80,80);
5.初始化CollectionView
UICollectionView *collectionView = [[UICollectionViewalloc] initWithFrame:CGRectMake(0,0, width, height) collectionViewLayout:flowLayout];
6.设置分页
collectionView.pagingEnabled= YES;
7.设置滑动条隐藏
collectionView.showsHorizontalScrollIndicator= NO;
collectionView.showsVerticalScrollIndicator = NO;
8.注册单元格
[collectionViewregisterClass:[ PhotoCell class]forCellWithReuseIdentifier:@"myCell"];
9.设置填充量
collectionView.contentInset = UIEdgeInsetsMake(0, (kScreenWidth -220) / 2, 0, (kScreenWidth -220) / 2);
10.设置scrollView的减速速率(范围0—1)
self.decelerationRate =UIScrollViewDecelerationRateFast
11.#pragma mark -UICollectionViewDataSource
//返回单元格的个数
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.data.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
{
return self.data.count;
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell"forIndexPath:indexPath];
cell.backgroundColor= [UIColorcolorWithRed:arc4random() % 10* 0.1 green:arc4random() %10 * 0.1blue:arc4random() % 10* 0.1 alpha:1];
cell.model= self.data[indexPath.row];
return cell;
}
12.动态设置单元格的尺寸
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
return CGSizeMake(80,arc4random() %80);
{
return CGSizeMake(80,arc4random() %80);
}
13.当单元格已经不在屏幕上显示时调用的方法(使图片还原)
- (void)collectionView:(UICollectionView*)collectionView didEndDisplayingCell:(PhotoCell*)cell forItemAtIndexPath:(NSIndexPath*)indexPath
{
cell.scrollView.zoomScale= 1;
{
cell.scrollView.zoomScale= 1;
}
14.设置点击到的图片水平居中
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
[collectionView scrollToItemAtIndexPath:indexPathatScrollPosition:UICollectionViewScrollPositionCenteredHorizontallyanimated:YES];
{
[collectionView scrollToItemAtIndexPath:indexPathatScrollPosition:UICollectionViewScrollPositionCenteredHorizontallyanimated:YES];
}
15.设置每个section初始化的偏移量
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, (kScreenWidth- self.pageWidth) /2,0, (kScreenWidth- self.pageWidth) /2);
{
return UIEdgeInsetsMake(0, (kScreenWidth- self.pageWidth) /2,0, (kScreenWidth- self.pageWidth) /2);
}
16.手指将要离开屏幕时调用的方法(scrollView:滑动对象;velocity:手指离开屏幕的时候,scrollView的滑动速度;targetContentOffset: scrollview停止后的偏移量)
- (void)scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inoutCGPoint*)targetContentOffset
{
}
17.结束显示单元格时调用的方法
- (void)collectionView:(UICollectionView*)collectionView didEndDisplayingCell:(PosterCell*)cell forItemAtIndexPath:(NSIndexPath*)indexPath
{
}
0 0
- UICollectionView——iOS学习连载27
- iOS学习之——UICollectionView详解
- iOS——UICollectionView
- iOS学习之—— UICollectionView导致的崩溃问题
- IOS UICollectionView学习
- iOS - UICollectionView学习
- iOS 深入学习UICollectionView
- 单例——iOS学习连载19
- 加载网页——iOS学习连载28
- ios开发——UICollectionView初阶
- iOS UICollectionView简介 —— HERO博客
- IOS学习 6新增-UICollectionView
- ios学习笔记:UICollectionView收集
- iOS学习笔记 UICollectionView入门
- iOS UICollectionView学习之一,UICollectionView + storyboard 简单应用
- UI高级第四课 iOS绘图与动画——iOS学习连载32
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- 项目一(电影APP)——iOS学习连载26
- 初识OpenGL
- 简析——NSFileManager与NSFileHandle
- 关于自学能力
- [leetcode] Group Anagrams
- UICollectionView——iOS学习连载27
- 一个字符串在另一个字符串中出现的次数
- 加载网页——iOS学习连载28
- CSS3 Media Queries Css媒体查询
- Android开发真机测试方法
- 谷歌返华,是“狼来了”还是狼来了?
- java线程死锁例子及解决方法
- C# Attribute(特性)之---数据契约 [DataContract]
- Data URL和图片