日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【OC】【一秒就会】【collectionView 头部吸住功能】

發(fā)布時(shí)間:2024/9/21 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【OC】【一秒就会】【collectionView 头部吸住功能】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??

貢獻(xiàn)作者 -【XJDomain】
博客XJ:? https://my.oschina.net/shengbingli/blog
GitHub:? https://github.com/lishengbing/XJQRCodeToolDemo

直接使用我的自定義類:

1:tableview有自動頭部和底部吸頂?shù)墓δ?#xff0c;collectionView也是可以做到的
2:將collectionView的 flowLayout 換成我的自定義類的對象即可

使用范例:

//固定頭部layout SectionHeadercCollectionViewLayout *layout = [[SectionHeadercCollectionViewLayout alloc] init];//不固定頭部 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

?

// // SectionHeadercCollectionViewLayout.h // XDSectionCollectionView // // Created by 李勝兵 on 15/9/9. // Copyright (c) 2015年 李勝兵. All rights reserved. //#import <UIKit/UIKit.h>@interface SectionHeadercCollectionViewLayout : UICollectionViewFlowLayout@end

?

// // SectionHeadercCollectionViewLayout.m // XDSectionCollectionView // // Created by 李勝兵 on 15/9/9. // Copyright (c) 2015年 李勝兵. All rights reserved. //#import "SectionHeadercCollectionViewLayout.h"/*** SectionHeader可以像tableView那樣懸浮在頂部* 繼承于UICollectionViewFlowLayout,用法與collectionViewFlowLayout一樣*/ @implementation SectionHeadercCollectionViewLayout- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {//UICollectionViewLayoutAttributes:我稱它為collectionView中的item(包括cell和header、footer這些)的《結(jié)構(gòu)信息》//截取到父類所返回的數(shù)組(里面放的是當(dāng)前屏幕所能展示的item的結(jié)構(gòu)信息),并轉(zhuǎn)化成不可變數(shù)組NSMutableArray *superArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];//創(chuàng)建存索引的數(shù)組,無符號(正整數(shù)),無序(不能通過下標(biāo)取值),不可重復(fù)(重復(fù)的話會自動過濾)NSMutableIndexSet *noneHeaderSections = [NSMutableIndexSet indexSet];//遍歷superArray,得到一個(gè)當(dāng)前屏幕中所有的section數(shù)組for (UICollectionViewLayoutAttributes *attributes in superArray){//如果當(dāng)前的元素分類是一個(gè)cell,將cell所在的分區(qū)section加入數(shù)組,重復(fù)的話會自動過濾if (attributes.representedElementCategory == UICollectionElementCategoryCell){[noneHeaderSections addIndex:attributes.indexPath.section];}}//遍歷superArray,將當(dāng)前屏幕中擁有的header的section從數(shù)組中移除,得到一個(gè)當(dāng)前屏幕中沒有header的section數(shù)組//正常情況下,隨著手指往上移,header脫離屏幕會被系統(tǒng)回收而cell尚在,也會觸發(fā)該方法for (UICollectionViewLayoutAttributes *attributes in superArray){//如果當(dāng)前的元素是一個(gè)header,將header所在的section從數(shù)組中移除if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]){[noneHeaderSections removeIndex:attributes.indexPath.section];}}//遍歷當(dāng)前屏幕中沒有header的section數(shù)組[noneHeaderSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop){//取到當(dāng)前section中第一個(gè)item的indexPathNSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];//獲取當(dāng)前section在正常情況下已經(jīng)離開屏幕的header結(jié)構(gòu)信息UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];//如果當(dāng)前分區(qū)確實(shí)有因?yàn)殡x開屏幕而被系統(tǒng)回收的headerif (attributes){//將該header結(jié)構(gòu)信息重新加入到superArray中去[superArray addObject:attributes];}}];//遍歷superArray,改變header結(jié)構(gòu)信息中的參數(shù),使它可以在當(dāng)前section還沒完全離開屏幕的時(shí)候一直顯示for (UICollectionViewLayoutAttributes *attributes in superArray) {//如果當(dāng)前item是headerif ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]){//得到當(dāng)前header所在分區(qū)的cell的數(shù)量NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:attributes.indexPath.section];//得到第一個(gè)item的indexPathNSIndexPath *firstItemIndexPath = [NSIndexPath indexPathForItem:0 inSection:attributes.indexPath.section];//得到最后一個(gè)item的indexPathNSIndexPath *lastItemIndexPath = [NSIndexPath indexPathForItem:MAX(0, numberOfItemsInSection-1) inSection:attributes.indexPath.section];//得到第一個(gè)item和最后一個(gè)item的結(jié)構(gòu)信息UICollectionViewLayoutAttributes *firstItemAttributes, *lastItemAttributes;if (numberOfItemsInSection>0){//cell有值,則獲取第一個(gè)cell和最后一個(gè)cell的結(jié)構(gòu)信息firstItemAttributes = [self layoutAttributesForItemAtIndexPath:firstItemIndexPath];lastItemAttributes = [self layoutAttributesForItemAtIndexPath:lastItemIndexPath];}else{//cell沒值,就新建一個(gè)UICollectionViewLayoutAttributesfirstItemAttributes = [UICollectionViewLayoutAttributes new];//然后模擬出在當(dāng)前分區(qū)中的唯一一個(gè)cell,cell在header的下面,高度為0,還與header隔著可能存在的sectionInset的topCGFloat y = CGRectGetMaxY(attributes.frame)+self.sectionInset.top;firstItemAttributes.frame = CGRectMake(0, y, 0, 0);//因?yàn)橹挥幸粋€(gè)cell,所以最后一個(gè)cell等于第一個(gè)celllastItemAttributes = firstItemAttributes;}//獲取當(dāng)前header的frameCGRect rect = attributes.frame;//當(dāng)前的滑動距離 + 因?yàn)閷?dǎo)航欄產(chǎn)生的偏移量,默認(rèn)為64(如果app需求不同,需自己設(shè)置)CGFloat offset = self.collectionView.contentOffset.y;//第一個(gè)cell的y值 - 當(dāng)前header的高度 - 可能存在的sectionInset的topCGFloat headerY = firstItemAttributes.frame.origin.y - rect.size.height - self.sectionInset.top;//哪個(gè)大取哪個(gè),保證header懸停//針對當(dāng)前header基本上都是offset更加大,針對下一個(gè)header則會是headerY大,各自處理CGFloat maxY = MAX(offset,headerY);//最后一個(gè)cell的y值 + 最后一個(gè)cell的高度 + 可能存在的sectionInset的bottom - 當(dāng)前header的高度//當(dāng)當(dāng)前section的footer或者下一個(gè)section的header接觸到當(dāng)前header的底部,計(jì)算出的headerMissingY即為有效值CGFloat headerMissingY = CGRectGetMaxY(lastItemAttributes.frame) + self.sectionInset.bottom - rect.size.height;//給rect的y賦新值,因?yàn)樵谧詈笙У呐R界點(diǎn)要跟誰消失,所以取小rect.origin.y = MIN(maxY,headerMissingY);//給header的結(jié)構(gòu)信息的frame重新賦值attributes.frame = rect;//如果按照正常情況下,header離開屏幕被系統(tǒng)回收,而header的層次關(guān)系又與cell相等,如果不去理會,會出現(xiàn)cell在header上面的情況//通過打印可以知道cell的層次關(guān)系zIndex數(shù)值為0,我們可以將header的zIndex設(shè)置成1,如果不放心,也可以將它設(shè)置成非常大,這里隨便填了個(gè)7attributes.zIndex = 7;}}//轉(zhuǎn)換回不可變數(shù)組,并返回return [superArray copy];}//return YES;表示一旦滑動就實(shí)時(shí)調(diào)用上面這個(gè)layoutAttributesForElementsInRect:方法 - (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound {return YES; }@end

?

如果你覺得對你有用,請你star,謝謝
如果你覺得以后對你還是有點(diǎn)用,也請你star、謝謝

// 強(qiáng)大的二維碼一鍵搞定
https://github.com/lishengbing/XJQRCodeToolDemo

// 強(qiáng)大的swift3.0版本側(cè)滑全功能、第三方庫都滿足不了我的要求
https://github.com/lishengbing/swift3.0-XJDragerDemo

// 強(qiáng)大的oc版本抽屜效果、第三方庫都滿足不了我的要求
https://github.com/lishengbing/XJDragViewController

轉(zhuǎn)載于:https://my.oschina.net/shengbingli/blog/793416

總結(jié)

以上是生活随笔為你收集整理的【OC】【一秒就会】【collectionView 头部吸住功能】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。