时间:2021-05-20
本文实例为大家分享了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 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的代理方法了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了iOStableview头部拉伸效果展示的具体代码,例如探探个人信息界面拉伸效果,下拉头像放大代码:////PersonController
IOS开发仿微信右侧弹出视图实现微信首页的+号,点击之后会弹出一个更多的视图,这个视图如何实现呢?实现该效果可能需要以下技术要点:1.图片拉伸,通过拉伸
本文实例讲述了JS实现横向拉伸动感伸缩菜单效果代码。分享给大家供大家参考。具体如下:这是一款JS实现的纵向拉伸变横向拉伸,动感伸缩菜单,紧身排列的CSS菜单,可
大家可能注意到有些tableView的顶部图片,会随着你拉伸而跟着拉伸变大。本文实例为大家分享了ios实现tableView顶部“弹簧”
可任意合并表头,实现单元格编辑。页面效果如图:页面使用如下:exportdefault{data(){return{tableData:[{'a':'1','b