iOS使用UICollectionView实现列表头部拉伸效果
2019/7/9 22:41:01
本文主要是介绍iOS使用UICollectionView实现列表头部拉伸效果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例为大家分享了iOS实现列表下拉放大效果展示的具体代码,供大家参考,具体内容如下
先看效果图
突然发现没有做出来之前都觉得蛮难的,做出来之后就觉得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 UIKit class 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的代理方法了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于iOS使用UICollectionView实现列表头部拉伸效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-12Axios库资料:新手入门必读教程
- 2024-11-11Axios库项目实战:新手入门教程
- 2024-09-29Axios库教程:初学者必备指南
- 2024-08-29Axios库资料:新手入门指南与基本使用教程
- 2024-03-14system bios shadowed
- 2024-03-14gabios
- 2024-02-07iOS应用提交上架的最新流程
- 2024-02-06打包 iOS 的 IPA 文件
- 2023-12-07uniapp打包iOS应用并通过审核:代码混淆的终极解决方案 ?
- 2023-11-25uniapp IOS从打包到上架流程(详细简单) 原创