【iOS开发系列】collectionView头部悬浮
2016-03-22 16:14
441 查看
// // WNCalendarCollectionLayout.h // Calendar // // Created by 付国良 on 16/3/22. // Copyright © 2016年 日历. All rights reserved. // #import <UIKit/UIKit.h> @interface WNCalendarCollectionLayout : UICollectionViewFlowLayout @property (nonatomic, assign) BOOL isSuspend; @end
// Calendar // // Created by 付国良 on 16/3/22. // Copyright © 2016年 日历. All rights reserved. // #import "WNCalendarCollectionLayout.h" @implementation WNCalendarCollectionLayout - (instancetype)init { self = [super init]; if (self){ } return self; } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; UICollectionView * const cv = self.collectionView; CGPoint const contentOffset = cv.contentOffset; NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet]; for (UICollectionViewLayoutAttributes *layoutAttributes in answer) { if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) { [missingSections addIndex:layoutAttributes.indexPath.section]; } } for (UICollectionViewLayoutAttributes *layoutAttributes in answer) { if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { [missingSections removeIndex:layoutAttributes.indexPath.section]; } } [missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx]; UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath]; [answer addObject:layoutAttributes]; }]; for (UICollectionViewLayoutAttributes *layoutAttributes in answer) { if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) { NSInteger section = layoutAttributes.indexPath.section; NSInteger numberOfItemsInSection = [cv numberOfItemsInSection:section]; NSIndexPath *firstObjectIndexPath = [NSIndexPath indexPathForItem:0 inSection:section]; NSIndexPath *lastObjectIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section]; UICollectionViewLayoutAttributes *firstObjectAttrs; UICollectionViewLayoutAttributes *lastObjectAttrs; if (numberOfItemsInSection > 0) { firstObjectAttrs = [self layoutAttributesForItemAtIndexPath:firstObjectIndexPath]; lastObjectAttrs = [self layoutAttributesForItemAtIndexPath:lastObjectIndexPath]; } else { firstObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:firstObjectIndexPath]; lastObjectAttrs = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:lastObjectIndexPath]; } CGFloat headerHeight = CGRectGetHeight(layoutAttributes.frame); CGPoint origin = layoutAttributes.frame.origin; origin.y = MIN( MAX( contentOffset.y + cv.contentInset.top, (CGRectGetMinY(firstObjectAttrs.frame) - headerHeight) ), (CGRectGetMaxY(lastObjectAttrs.frame) - headerHeight) ); layoutAttributes.zIndex = 1024; layoutAttributes.frame = (CGRect){ .origin = origin, .size = layoutAttributes.frame.size }; } } return answer; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBound { return _isSuspend; } - (BOOL)isSuspend{ return _isSuspend; } @end
相关文章推荐
- iOS 开发之照片框架详解(3)
- iOS 开发之照片框架详解(2)
- iOS 开发之照片框架详解(1)
- iOS中ProtocolBuffer2.2的安装及google/protobuf/message.cc:130:60错误解决办法
- iOS 应用内付费(IAP)开发步骤
- iOS崩溃收集原理
- iOS收起键盘
- iOS开发——导入第三方库引起的unknown type name 'NSString'
- iOS开发证书转移
- iOS 身份证验证(转)
- stringByAddingPercentEscapesUsingEncoding:《iOS9中禁用》
- [ios]使用self.view.window.rootViewController presentViewControllert跳转后的返回
- 【即时通讯】iOS 基于GCDAsyncSocket快速开发Socket通信
- ios 第三方分享组件
- Mac OS X和IOS上基本数据类型的字节数
- iOS 调用系统相机和相册
- iOS9中-被禁用的方法
- iOS代码片段CodeSnippets
- 网易云捕:一款iOS平台下方便好用的崩溃收集统计系统
- 项目篇 - 项目技巧