国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS使用UICollectionView實(shí)現(xiàn)列表頭部拉伸效果

2019-10-21 18:41:11
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了iOS實(shí)現(xiàn)列表下拉放大效果展示的具體代碼,供大家參考,具體內(nèi)容如下

先看效果圖

iOS,UICollectionView,列表頭部拉伸

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

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

OC版本

創(chuàng)建一個類 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

在控制器中使用 先導(dǎo)入頭文件

// 創(chuàng)建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;  //設(shè)置代理  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 不需要導(dǎo)入頭文件那么麻煩

//// 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方法實(shí)現(xiàn)

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)

注:不要實(shí)現(xiàn)UICollectionViewDelegateFlowLayout的代理方法了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 周宁县| 长武县| 上犹县| 梅州市| 丹阳市| 新兴县| 江门市| 信丰县| 云梦县| 辽中县| 昭觉县| 五河县| 大埔县| 镇原县| 萝北县| 南宁市| 沙洋县| 长沙市| 禹州市| 东安县| 巨野县| 包头市| 玉田县| 灵武市| 华蓥市| 绩溪县| 改则县| 焉耆| 英吉沙县| 杭州市| 灵川县| 巫山县| 宁武县| 福安市| 贵阳市| 沙湾县| 筠连县| 和田市| 锡林郭勒盟| 东光县| 景宁|