麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS使用UICollectionView實現列表頭部拉伸效果

2019-10-21 18:41:11
字體:
來源:轉載
供稿:網友

本文實例為大家分享了iOS實現列表下拉放大效果展示的具體代碼,供大家參考,具體內容如下

先看效果圖

iOS,UICollectionView,列表頭部拉伸

突然發現沒有做出來之前都覺得蠻難的,做出來之后就覺得So Easy 大家都有這樣的感觸吧

做這個就重寫 UICollectionViewFlowLayout 的幾個方法就可以

OC版本

創建一個類 CustomCollectionViewFlowLayout 繼承 UICollectionViewFlowLayout

//// CustomCollectionViewFlowLayout.m// //// Created by GongHui_YJ on 16/8/4.// Copyright © 2016年 Yangjian. All rights reserved.//#import "CustomCollectionViewFlowLayout.h"@implementation CustomCollectionViewFlowLayout- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{  return YES;}- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {  UICollectionView *collectionView = [self collectionView];  UIEdgeInsets insets = [collectionView contentInset];  CGPoint offset = [collectionView contentOffset];  CGFloat minY = -insets.top;  NSArray *attributes = [super layoutAttributesForElementsInRect:rect];  if (offset.y < minY) {    CGSize headerSize = [self headerReferenceSize];    CGFloat deltaY = fabsf(offset.y - minY);    for (UICollectionViewLayoutAttributes *attrs in attributes) {      if ([attrs representedElementKind] == UICollectionElementKindSectionHeader) {        CGRect headerRect = [attrs frame];        headerRect.size.height = MAX(minY, headerSize.height + deltaY);        headerRect.origin.y = headerRect.origin.y - deltaY;        [attrs setFrame:headerRect];        break;      }    }  }  return attributes;}@end

在控制器中使用 先導入頭文件

// 創建collectionView  CustomCollectionViewFlowLayout *flowLayout=[[CustomCollectionViewFlowLayout alloc] init];  [flowLayout setSectionInset:UIEdgeInsetsMake(0, 0, 10, 0)];  [flowLayout setItemSize:CGSizeMake(kScreenWidth / collectionCellW, kScreenWidth / collectionCellW)];  [flowLayout setHeaderReferenceSize:CGSizeMake(kScreenWidth, userInfoImageViewH)];  [flowLayout setFooterReferenceSize:CGSizeMake(kScreenWidth, 83)];  [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];  [flowLayout setMinimumInteritemSpacing:0.0];  [flowLayout setMinimumLineSpacing:0.0];  self.homeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)collectionViewLayout:flowLayout];  self.homeCollectionView.backgroundColor = kViewBackgroundColor;  self.homeCollectionView.alwaysBounceVertical = YES;  self.homeCollectionView.showsVerticalScrollIndicator = NO;  //設置代理  self.homeCollectionView.delegate = self;  self.homeCollectionView.dataSource = self;  [self.view addSubview:self.homeCollectionView];  // 注冊表頭  [self.homeCollectionView registerClass:[YJHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionHeaderView];  // 注冊表尾  [self.homeCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionFooterView];

Swift版

喜歡swift 不需要導入頭文件那么麻煩

//// CustomCollectionViewFlowLayout.swift// //// Created by GongHui_YJ on 16/8/4.// Copyright © 2016年YangJian. All rights reserved.//import UIKitclass CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {  override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {    return true  }  override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {    let collectionView = self.collectionView    let insets = collectionView?.contentInset    let offset = collectionView?.contentOffset    let minY = -((insets?.top)!)    let attributesArray = super.layoutAttributesForElementsInRect(rect)    if offset!.y < minY {      let headerSize = self.headerReferenceSize      let deltaY = CGFloat(fabsf(Float((offset?.y)! - CGFloat(minY))))      for attrs:UICollectionViewLayoutAttributes in attributesArray! {        if attrs.representedElementKind == UICollectionElementKindSectionHeader {          var headerRect = attrs.frame          headerRect.size.height = max(minY, headerSize.height + deltaY)          headerRect.origin.y = headerRect.origin.y - deltaY          attrs.frame = headerRect          break        }      }    }    return attributesArray  }}

在控制器 viewDidLoad方法實現

let customFlowLayout = CustomCollectionViewFlowLayout()  customFlowLayout.headerReferenceSize = CGSizeMake(kScreenWidth, 203)  customFlowLayout.footerReferenceSize = CGSizeMake(kScreenWidth, 83)  customFlowLayout.minimumInteritemSpacing = 0  customFlowLayout.minimumLineSpacing = 0  customFlowLayout.itemSize = CGSizeMake(kScreenWidth / 3.000006, kScreenWidth / 3.00006)  customFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 10, 0)  self.homeCollectionView.setCollectionViewLayout(customFlowLayout, animated: true)  self.homeCollectionView.backgroundColor = kViewBackgroundColor  self.homeCollectionView.alwaysBounceVertical = true  let nib = UINib(nibName: "CommonCollectionViewCell", bundle: nil)  self.homeCollectionView.registerNib(nib, forCellWithReuseIdentifier: cellId)  // 注冊表頭表尾  let headerNib = UINib(nibName: "HeaderCollectionReusableView", bundle: nil)  self.homeCollectionView.registerNib(headerNib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: collectionHeaderId)  self.homeCollectionView.registerClass(UICollectionReusableView.classForCoder(), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: collectionFooterId)

注:不要實現UICollectionViewDelegateFlowLayout的代理方法了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 免费99热在线观看 | 久久亚洲成人 | 91精品国产刺激国语对白 | 亚洲片在线观看 | 亚洲天堂岛国片 | 草妞视频 | 久久美女色视频 | 免费一级毛片电影 | 色婷婷久久久久久 | 久久久精品网 | 超碰99在线观看 | 国产色91| 日本精品视频一区二区三区四区 | av在线看网站 | 超碰人人做人人爱 | 久草最新网址 | 在线看国产视频 | 一级做受毛片免费大片 | 91在线精品亚洲一区二区 | 久草在线视频新 | 国产免费久久久久 | 久久国产精品二区 | 久久久久免费精品 | 国产精品视频中文字幕 | 538在线精品 | 色域tv | 精品亚洲午夜久久久久91 | 伊人久久国产精品 | 久久精精 | 国产高潮好爽好大受不了了 | 国产深夜福利视频在线播放 | 久久另类视频 | 久久久久97国产精 | 成人在线视频一区 | 国产在线免 | 一级大片一级一大片 | 久精品国产 | 亚洲欧美日韩久久精品第一区 | 欧美激情综合在线 | 五月天堂婷婷 | 视频一区二区精品 |