基本设置UICollectionView

自定义单元格UICollectionViewCell

自定义布局UICollectionViewFlowLayout//流式布局

1. 创建

//多行多列的表格视图在创建的时候,必须指定单元格的布局
[[HARFirstCVC alloc]initWithCollectionViewLayout:[[HARMylayout alloc]init]];

- (instancetype)init

{

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    // cell的大小

    layout.itemSize = CGSizeMake(305, 305);

    return [self initWithCollectionViewLayout:layout];

}

2. 属性

//设置backgroundView属性

self.backgroundView=self.bgImageView;

//设置单元格大小

self.itemSize=CGSizeMake(80, 80);

//设置单元格间距

self.minimumInteritemSpacing=10;

//设置单元格行间距

self.minimumLineSpacing=10;

//设置区间隔 上左下右

self.sectionInset=UIEdgeInsetsMake(110, 30, 110, 30);

//设置滚动方向为水平滚动

self.scrollDirection=UICollectionViewScrollDirectionHorizontal;

//设置竖直方向的弹簧效果

self.collectionView.alwaysBounceVertical=YES;

3. 方法

// 往contentView中添加label

[self.contentView addSubview:label];

// 获得第一个区的行数

[self.collectionView numberOfItemsInSection:0]

// 通过cell获取坐标

[self.collectionView indexPathForCell:sender];

3.1 设置标头

//设置大小

flowLayout.headerReferenceSize=CGSizeMake(200, 20);

//注册

[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

//写代理方法

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    UICollectionReusableView *reusableView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

    reusableView.backgroundColor=[UIColor blueColor];

    return reusableView;

}

4. 代理方法

UICollectionViewDelegateFlowLayout

//四周边距

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(10, 10, 10, 10);

}

//行间距

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 10;

}

//列间距

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    return 10;

}

//每个Cell的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    CGFloat width = (self.view.bounds.size.width - 4*10)/3;

//    220 * 365 宽*高

    CGFloat height = width * 365.0/220.0;

    return CGSizeMake(width, height);

}

5. 
UICollectionViewFlowLayout

6. 常用自定义布局

6.1 滚动到屏幕中点位置放大,原理则缩小

// 将父类原本生成的用于对各个项进行定位的attributes属性,按照我们指定的变化规则,修改attributes中与放大缩小有关的那个属性

// 返回一个矩形区域中的所有小秘书

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

   //通过父类方法,得到小秘书数组

   NSArray *array=[super layoutAttributesForElementsInRect:rect];

   //获取可视化区域的fram

   CGRect visibleRect=CGRectZero;    visibleRect.origin=self.collectionView.contentOffset;               

    visibleRect.size=self.collectionView.bounds.size;
   //获取可视化区域的横向中点

   CGFloat visibleRectCenterX=CGRectGetMidX(visibleRect);

   //遍历数组,得到小秘书

   for (UICollectionViewLayoutAttributes *attributes in array) {

      //得到小秘书中,中点的横向坐标

      CGFloat itemCenterX=attributes.center.x;

      //计算得到可见区域中点与该小秘书中点的横向差值

      CGFloat distance=visibleRectCenterX-itemCenterX;

      //如果横向距离小于200,进行变形

      if (ABS(distance)<200) {

         //变形值为1-1.5,距离200是,不变形,0时放大1.5倍

         CGFloat zoomFactor=1+0.5*(1-ABS(distance)/200);

         //设置小秘书的3D属性中宽高的变形比         

         attributes.transform3D=CATransform3DMakeScale(zoomFactor, zoomFactor, 1);

      }

   }

   return array;

}

//当对象大小发生改变时,是否显示

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{

    return YES;

}

6.2 瀑布流

//设置内容区域的大小

-(CGSize)collectionViewContentSize{

    return CGSizeMake(self.collectionView.bounds.size.width, ([self.collectionView numberOfItemsInSection:0]/3+1)*200);

}

//根据item的indexPah返回具体的布局属性对象

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

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{}

6.3 弹簧特效

-(void)prepareLayout{

    if (_animator==nil) {

        _animator=[[UIDynamicAnimator alloc]initWithCollectionViewLayout:self];

        
//得到整个collectionView的尺寸
        CGSize 
contentSize=self.collectionViewContentSize;
        //获得区域内的所有对象的attributes

        NSArray *layoutAttributes=[super layoutAttributesForElementsInRect:CGRectMake(0, 0, contentSize.width, contentSize.height)];

        //遍历,为所有的sttributes添加吸附特效

        for (UICollectionViewLayoutAttributes *attributes in layoutAttributes) {

            UIAttachmentBehavior *attach=[[UIAttachmentBehavior alloc]initWithItem:attributes attachedToAnchor:attributes.center];

            attach.damping=0.6;

            attach.frequency=0.8;

            [self.animator addBehavior:attach];

        }

    }

}

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

    //返回经过处理的attributes的数组

    return [self.animator itemsInRect:rect];    

}

//当边界发生变化时,不更新边界,针对用户的本次滚动距离,修改吸附效果的锚点位置,以此产生弹簧效果

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{

    
//获得移动的距离    
    CGFloat distance=self.collectionView.bounds.origin.y-newBounds.origin.y;

    
//获得触点的坐标
    CGPoint touchLocation=[self.collectionView.panGestureRecognizer locationInView:self.collectionView];

    
//遍历特效场所,获得特效
    for (UIAttachmentBehavior *attach in self.animator.behaviors) {

        
//通过特效,获得特效绑定的那个对象,也就是attributes
        UICollectionViewLayoutAttributes *attributes=[attach.items firstObject];

        CGPoint center=attributes.center;//获得中心店坐标

        CGPoint anchorPoint=attach.anchorPoint;//获得描点坐标

        CGFloat distance2=fabsf(touchLocation.y-anchorPoint.y);//获得触点到描点的距离

        center.y+=distance*distance2/500;

        attributes.center=center;

        [self.animator updateItemUsingCurrentState:attributes];

    }

    return NO;

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容