亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

OC-29.自定義布局實現瀑布流

2019-11-14 17:52:57
字體:
來源:轉載
供稿:網友

概述


  • 瀑布流是電商應用展示商品通常采用的一種方式,如圖示例

  • 瀑布流的實現方式,通常有以下幾種

    • 通過UITableView實現(不常用)
    • 通過UIScrollView實現(工作量較大)
    • 通過UICollectionView實現(通常采用的方式)

UICollectionView基礎


  • UICollectionView與UITableView有很多相似的地方,如

    • 都通過數據源提供數據
    • 都通過代理執行相關的事件
    • 都可以自定義cell,且涉及到cell的重用
    • 都繼承自UIScrollView,具有滾動效果
  • UICollectionView的特性

    • 需要有一個UICollectionViewLayout類(通常是UICollectionViewFlowLayout類)或其子類對象,來決定cell的布局
    • 可以實現UICollectionViewLayout的子類,來定制UICollectionView的滾動方向以及cell的布局
  • UICollectionViewLayout的子類UICollectionViewFlowLayout

    • UICollectionViewFlowLayout即流水布局
    • 流水布局UICollectionView的最常用的一種布局方式

自定義布局


  • 自定義布局需要實現UICollectionViewLayout的子類
  • 自定義布局常用方法

    • 初始化布局

      - (void)PRepareLayout{	//通常在該方法中完成布局的初始化操作}
    • 當尺寸改變時,是否更新布局

      - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{	//默認返回YES}
    • 布局UICollectionView的元素

      - (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{	//該方法需要返回rect區域中所有元素布局屬性的數組}- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{	//該方法返回indexPath位置的元素的布局屬性}
    • 修改UICollectionView停止滾動是的偏移量

      - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{	//返回值是,UICollectionView最終停留的點}

示例


  • 實現效果

  • 實現思路

    • 通過UICollectionView實現
    • 自定義布局,即橫向流水布局和圓形普通布局
    • 通過UICollectionView的代理方法,當點擊cell時,將其刪除
    • 通過監聽UIView的觸摸事件,當點解控制器的view時更改布局
  • 實現步驟

    • 自定橫向流水布局

      • 初始化布局

        - (void)prepareLayout{    [super prepareLayout];    //設置滾動方向    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;    //設置內邊距    CGFloat inset = (self.collectionView.frame.size.width - self.itemSize.width) * 0.5;    self.sectionInset = UIEdgeInsetsMake(0, inset, 0, inset);}
      • 指定當尺寸改變時,更新布局

        - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{    return YES;}
      • 設置所有元素的布局屬性

        - (nullable NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{    //獲取rect區域中所有元素的布局屬性    NSArray *array = [super layoutAttributesForElementsInRect:rect];    //獲取UICollectionView的中點,以contentView的左上角為原點    CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width * 0.5;    //重置rect區域中所有元素的布局屬性,即基于他們距離UICollectionView的中點的劇烈,改變其大小    for (UICollectionViewLayoutAttributes *attribute in array)    {        //獲取距離中點的距離        CGFloat delta = ABS(attribute.center.x - centerX);        //計算縮放比例        CGFloat scale = 1 - delta / self.collectionView.bounds.size.width;        //設置布局屬性        attribute.transform = CGAffineTransformMakeScale(scale, scale);    }    //返回所有元素的布局屬性    return [array copy];}
      • 設置UICollectionView停止滾動是的偏移量,使其為與中心點

        - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{    //計算最終顯示的矩形框    CGRect rect;    rect.origin.x = proposedContentOffset.x;    rect.origin.y = 0;    rect.size = self.collectionView.frame.size;    //獲取最終顯示在矩形框中的元素的布局屬性    NSArray *array = [super layoutAttributesForElementsInRect:rect];    //獲取UICollectionView的中點,以contentView的左上角為原點    CGFloat centerX = proposedContentOffset.x + self.collectionView.frame.size.width * 0.5;    //獲取所有元素到中點的最短距離    CGFloat minDelta = MAXFLOAT;    for (UICollectionViewLayoutAttributes *attribute in array)    {        CGFloat delta = attribute.center.x - centerX;        if (ABS(minDelta) > ABS(delta))        {            minDelta = delta;        }    }    //改變UICollectionView的偏移量    proposedContentOffset.x += minDelta;    return proposedContentOffset;}
    • 自定義圓形普通布局

      • 定義成員屬性,保存所有的布局屬性

        @property (nonatomic, strong) NSMutableArray *attrsArray;
      • 懶加載,初始化attrsArray

        - (NSMutableArray *)attrsArray{    if (_attrsArray == nil)    {        _attrsArray = [NSMutableArray array];    }    return _attrsArray;}
      • 初始化布局

        - (void)prepareLayout{    [super prepareLayout];    //移除所有舊的布局屬性    [self.attrsArray removeAllObjects];    //獲取元素的個數    NSInteger count = [self.collectionView numberOfItemsInSection:0];    //布局所有的元素    for (NSInteger i = 0; i<count; i++)    {        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];        //設置并獲取indexPath位置元素的布局屬性        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];        //將indexPath位置元素的布局屬性添加到所有布局屬性數組中        [self.attrsArray addObject:attrs];    }}
      • 布局indexPath位置的元素

        - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{    //獲取元素的個數    NSInteger count = [self.collectionView numberOfItemsInSection:0];    /**設置圓心布局*/    //設置圓形的半徑    CGFloat radius = 70;    //圓心的位置    CGFloat oX = self.collectionView.frame.size.width * 0.5;    CGFloat oY = self.collectionView.frame.size.height * 0.5;    //獲取indexPath位置的元素的布局屬性    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];    //設置尺寸    attrs.size = CGSizeMake(50, 50);    //設置位置    if (count == 1)    {        attrs.center = CGPointMake(oX, oY);    }    else    {        CGFloat angle = (2 * M_PI / count) * indexPath.item;        CGFloat centerX = oX + radius * sin(angle);        CGFloat centerY = oY + radius * cos(angle);        attrs.center = CGPointMake(centerX, centerY);    }    //返回indexPath位置元素的布局屬性    return attrs;}
      • 布局指定區域內所有的元素

        - (nullable NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{    //返回所有元素的布局屬性    return self.attrsArray;}
    • 通過xib自定義ce ll

      • 設置成員屬性,保存cell內部的圖片

        /**圖片名字*/@property (nonatomic, copy) NSString *imageName;
      • 初始化cell

        - (void)awakeFromNib{    //通過Layer設置邊框    self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;    self.imageView.layer.borderWidth = 6;    self.imageView.layer.cornerRadius = 3;}
      • 設置cell內imageView的image屬性

        - (void)setImageName:(NSString *)imageName{    _imageName = [imageName copy];    self.imageView.image = [UIImage imageNamed:imageName];}
    • 加載圖片資源

      • 通過成員屬性,保存所有的圖片名

        /**所有的圖片*/@property (nonatomic, strong) NSMutableArray *imageNames;
      • 懶加載,初始化圖片名數組

        - (NSMutableArray *)imageNames{    if (_imageNames == nil)    {        NSMutableArray *imageNames = [NSMutableArray array];        for (NSInteger i = 0; i<20; i++)        {            NSString *imageName = [NSString stringWithFormat:@"%zd", i + 1];            [imageNames addObject:imageName];        }        _imageNames = imageNames;    }    return _imageNames;}
    • 創建UICollectionView

      • 通過成員屬性保存UICollectionView對象,以便更改布局

        @property (nonatomic, weak) UICollectionView *collectionView;
      • 創建并設置collectionView

        - (void)setupCollectionView{    //設置frame    CGFloat collectionViewW = self.view.bounds.size.width;    CGFloat collectionViewH = 200;    CGRect frame = CGRectMake(0, 150, collectionViewW, collectionViewH);    //創建布局    LYPCircleLayout *layout = [[LYPCircleLayout alloc] init];    //創建collectionView    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];    self.collectionView = collectionView;    //設置collectionView的數據源和代理    collectionView.dataSource = self;    collectionView.delegate = self;    //添加collectionView到控制器的view中    [self.view addSubview:collectionView];}
    • 實現UICollectionView的數據源方法

      • 注冊cell

        /**設置重用表示*/static NSString *const ID = @"photo";- (void)viewDidLoad{    [super viewDidLoad];    [self setupCollectionView];    //注冊cell    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LYPPhotoCell class]) bundle:nil] forCellWithReuseIdentifier:ID];}
      • 設置元素的個數

        - (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return self.imageNames.count;}
      • 設置每個元素的屬性

        - (UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{    //根據重用標示從緩存池中取出cell,若緩存池中沒有,則自動創建    LYPPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];    //設置cell的imageName屬性    cell.imageName = self.imageNames[indexPath.item];    //返回cell    return cell;}
    • 實現UICollectionView的代理方法,實現點擊某個元素將其刪除功能

      - (void)collectionView:(nonnull UICollectionView *)collectionView didSelectItemAtIndexPath:(nonnull NSIndexPath *)indexPath{	//將圖片名從數組中移除    [self.imageNames removeObjectAtIndex:indexPath.item];    //刪除collectionView中的indexPath位置的元素    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];}
    • 監聽控制器view的點擊,更換布局

      - (void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{	//判斷當前布局的種類    if ([self.collectionView.collectionViewLayout isKindOfClass:[LYPLineLayout class]])    {    	//流水布局,切換至圓形布局        [self.collectionView setCollectionViewLayout:[[LYPCircleLayout alloc] init] animated:YES];    } else    {    	//圓形布局,切換至流水布局        LYPLineLayout *layout = [[LYPLineLayout alloc] init];        //設置元素的尺寸,若不設置,將使用自動計算尺寸        layout.itemSize = CGSizeMake(130, 130);        [self.collectionView setCollectionViewLayout:layout animated:YES];      }}

       

 
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
亚洲人成网站免费播放| 国产福利视频一区二区| 一区二区三区黄色| 性色av一区二区三区免费| 亚洲色无码播放| 日韩av大片在线| 日韩免费在线观看视频| 久久精品国产精品亚洲| 国产性猛交xxxx免费看久久| 91精品国产综合久久久久久蜜臀| 国产一区二区精品丝袜| 九九热精品视频| 国产精品久久久久久久午夜| 国产福利精品视频| 亚洲国产小视频在线观看| 九九综合九九综合| 亚洲第一男人av| 精品久久久久久久久久久久| 欧美日韩国产一区中文午夜| 久久免费视频在线| 国产成人精品一区二区三区| 亚洲一区二区自拍| 性色av一区二区咪爱| 综合欧美国产视频二区| 日本久久久久亚洲中字幕| 国产在线观看91精品一区| 欧美电影免费观看高清完整| 日韩一二三在线视频播| 91精品视频在线免费观看| 国产91精品网站| 国产福利视频一区二区| 91在线免费看网站| 97久久国产精品| 亚洲老头同性xxxxx| 精品偷拍各种wc美女嘘嘘| 国产日韩在线观看av| 精品女厕一区二区三区| 久久久久久久一| 亚洲剧情一区二区| 欧美床上激情在线观看| 日韩欧美999| 国产精品96久久久久久又黄又硬| 亚洲欧美日韩一区二区在线| 欧美激情影音先锋| 亚洲性线免费观看视频成熟| 亚洲欧美另类自拍| 国产一区二区黑人欧美xxxx| 国内精久久久久久久久久人| 日韩动漫免费观看电视剧高清| 日韩欧美在线观看视频| 中文字幕国产亚洲2019| 国产精品电影在线观看| 亚洲欧美制服综合另类| 日韩精品中文字幕视频在线| 97avcom| 欧美中文字幕第一页| 国产啪精品视频网站| 国产午夜精品免费一区二区三区| 久久国产精品电影| 欧洲一区二区视频| 国产精品老女人视频| 国产成人精品av| 日韩av中文字幕在线免费观看| 91在线视频精品| 久久中文久久字幕| 亚洲精品美女在线观看| 26uuu日韩精品一区二区| 一本色道久久88综合日韩精品| 国产欧美日韩精品在线观看| 日本亚洲精品在线观看| 亚洲美女中文字幕| 日韩在线一区二区三区免费视频| 亚洲日韩欧美视频一区| 日本久久久久亚洲中字幕| 中文字幕久热精品视频在线| 久久91超碰青草是什么| 91国产视频在线播放| 97久久精品在线| 欧美大全免费观看电视剧大泉洋| 久久99国产精品自在自在app| 国产欧美精品一区二区三区-老狼| 川上优av一区二区线观看| 欧美专区日韩视频| 欧美日本国产在线| 国产一区二区三区在线播放免费观看| 欧美另类高清videos| 亚洲综合国产精品| 亚洲国产成人久久综合一区| 久久久久久久国产精品视频| 91po在线观看91精品国产性色| 亚洲国产欧美自拍| 国产成人avxxxxx在线看| 国产成人一区二区三区| 欧美亚洲第一页| 91av在线免费观看| 色阁综合伊人av| 久久久成人精品视频| 欧美色播在线播放| 日本一区二区在线播放| 欧美大片欧美激情性色a∨久久| 久久久精品久久| 国产精品久久久久久久久免费| 国产精品视频一区国模私拍| 亚洲一区中文字幕在线观看| 伦理中文字幕亚洲| 欧美激情18p| 成人h猎奇视频网站| 久久综合国产精品台湾中文娱乐网| 亚洲欧美一区二区精品久久久| 欧美激情xxxx性bbbb| 伊人久久男人天堂| 久久久www成人免费精品张筱雨| 91麻豆国产语对白在线观看| 国产精品jvid在线观看蜜臀| 日韩中文字幕欧美| 91免费在线视频网站| 精品亚洲一区二区三区在线播放| 日韩免费av在线| 日韩国产高清视频在线| 亚洲色图国产精品| 国产午夜精品全部视频在线播放| 欧美丝袜一区二区| 国产这里只有精品| 国产69精品久久久久9| 日韩欧美极品在线观看| 久久久亚洲欧洲日产国码aⅴ| 亚洲国内精品在线| 久久精品国产一区二区三区| 欧美精品在线播放| 亚洲天堂男人天堂| 中文字幕v亚洲ⅴv天堂| 日本一区二区不卡| 日韩的一区二区| 久久久噜噜噜久久久| 国产精自产拍久久久久久蜜| www国产亚洲精品久久网站| 三级精品视频久久久久| 亚洲精品国产精品国自产观看浪潮| 亚洲日本欧美日韩高观看| 庆余年2免费日韩剧观看大牛| 国产精品久久久久久五月尺| 欧美日韩激情视频| 国产精品一区二区三区久久| 亚洲性xxxx| 久久夜色精品亚洲噜噜国产mv| 欧美午夜精品久久久久久浪潮| 疯狂蹂躏欧美一区二区精品| 久久久久久久国产精品| 最近2019中文字幕大全第二页| 国产999精品视频| 成人亲热视频网站| 激情久久av一区av二区av三区| 国产欧美一区二区三区久久| 亚洲精选中文字幕| 久久久久久久久中文字幕| 精品日本高清在线播放| 亚洲国产中文字幕在线观看| 亚洲男女性事视频| 久久亚洲精品小早川怜子66| 欧美电影电视剧在线观看| 久久夜色精品国产亚洲aⅴ| 国产精品电影在线观看| 成人美女av在线直播| 欧美激情中文网|