關(guān)于UICollectionView橫向分頁滾動(dòng),cell左右排版功能的實(shí)現(xiàn)

實(shí)現(xiàn)了對(duì)UICollectionView橫向滑動(dòng)的時(shí)候UICollectionViewCell橫向排列的功能优妙,并帶有分組功能
先看下效果圖

未命名.gif

廢話不多說直接上代碼

#import <UIKit/UIKit.h>

@interface ELCVFlowLayout : UICollectionViewFlowLayout

@end
#import "ELCVFlowLayout.h"

@interface ELCVFlowLayout ()

@property (nonatomic, copy) NSMutableDictionary *sectionDic;
@property (strong, nonatomic) NSMutableArray *allAttributes;

@end

@implementation ELCVFlowLayout


- (instancetype)init
{
   self = [super init];
   if (self) {
       self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
   }
   return self;
}


- (void)prepareLayout
{
   [super prepareLayout];

   _sectionDic = [NSMutableDictionary dictionary];
   self.allAttributes = [NSMutableArray array];
   //獲取section的數(shù)量
   NSUInteger section = [self.collectionView numberOfSections];
   
   for (int sec = 0; sec < section; sec++) {
       //獲取每個(gè)section的cell個(gè)數(shù)
       NSUInteger count = [self.collectionView numberOfItemsInSection:sec];
       
       for (NSUInteger item = 0; item<count; item++) {
           NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:sec];
           //重新排列
           UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
           [self.allAttributes addObject:attributes];

       }
   }

}

- (CGSize)collectionViewContentSize
{

   //每個(gè)section的頁碼的總數(shù)
   NSInteger actualLo = 0;
   for (NSString *key in [_sectionDic allKeys]) {
       actualLo += [_sectionDic[key] integerValue];
   }
   
   
   return CGSizeMake(actualLo*self.collectionView.frame.size.width, self.collectionView.contentSize.height);
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes
{
   
   if(attributes.representedElementKind != nil)
   {
       return;
   }
   
   /*修改by lixinkai 2017.6.30
   下面這兩個(gè)方法 itemW种蘸、itemH
    解決了同一個(gè)UICollectionView使用不同UICollectionViewCell的問題
    */
   //attributes 的寬度
   CGFloat itemW = attributes.frame.size.width;
   //attributes 的高度
   CGFloat itemH = attributes.frame.size.height;
   
   //collectionView 的寬度
   CGFloat width = self.collectionView.frame.size.width;
   //collectionView 的高度
   CGFloat height = self.collectionView.frame.size.height;
   //每個(gè)attributes的下標(biāo)值 從0開始
   NSInteger itemIndex = attributes.indexPath.item;
   
   CGFloat stride = (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) ? width : height;
   
   
   //獲取現(xiàn)在的attributes是第幾組
   NSInteger section = attributes.indexPath.section;
   //獲取每個(gè)section的item的個(gè)數(shù)
   NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];

   
   CGFloat offset = section * stride;
   
   //計(jì)算x方向item個(gè)數(shù)
   NSInteger xCount = (width / itemW);
   //計(jì)算y方向item個(gè)數(shù)
   NSInteger yCount = (height / itemH);
   //計(jì)算一頁總個(gè)數(shù)
   NSInteger allCount = (xCount * yCount);
   //獲取每個(gè)section的頁數(shù)年枕,從0開始
   NSInteger page = itemIndex / allCount;
   
   //余數(shù),用來計(jì)算item的x的偏移量
   NSInteger remain = (itemIndex % xCount);
   
   //取商贮喧,用來計(jì)算item的y的偏移量
   NSInteger merchant = (itemIndex-page*allCount)/xCount;


   //x方向每個(gè)item的偏移量
   CGFloat xCellOffset = remain * itemW;
   //y方向每個(gè)item的偏移量
   CGFloat yCellOffset = merchant * itemH;
   
   //獲取每個(gè)section中item占了幾頁
   NSInteger pageRe = (itemCount % allCount == 0)? (itemCount / allCount) : (itemCount / allCount) + 1;
   //將每個(gè)section與pageRe對(duì)應(yīng)栅盲,計(jì)算下面的位置
   [_sectionDic setValue:@(pageRe) forKey:[NSString stringWithFormat:@"%ld", section]];
   
   if(self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
       
       NSInteger actualLo = 0;
       //將每個(gè)section中的頁數(shù)相加
       for (NSString *key in [_sectionDic allKeys]) {
           actualLo += [_sectionDic[key] integerValue];
       }
       //獲取到的最后的數(shù)減去最后一組的頁碼數(shù)
       actualLo -= [_sectionDic[[NSString stringWithFormat:@"%ld", [_sectionDic allKeys].count-1]] integerValue];
       xCellOffset += page*width + actualLo*width;

   } else {
       
       yCellOffset += offset;
   }
  
   attributes.frame = CGRectMake(xCellOffset, yCellOffset, itemW, itemH);
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
//    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes  layoutAttributesForCellWithIndexPath:indexPath];
  UICollectionViewLayoutAttributes *attr = [super layoutAttributesForItemAtIndexPath:indexPath].copy;
   
   [self applyLayoutAttributes:attr];
   return attr;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
   return self.allAttributes;
}


@end

也可以直接下載
https://github.com/elite-kai/ELCVFlowLayout

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市白胀,隨后出現(xiàn)的幾起案子椭赋,更是在濱河造成了極大的恐慌,老刑警劉巖或杠,帶你破解...
    沈念sama閱讀 216,692評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件哪怔,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)认境,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門胚委,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人叉信,你說我怎么就攤上這事亩冬。” “怎么了硼身?”我有些...
    開封第一講書人閱讀 162,995評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵鉴未,是天一觀的道長。 經(jīng)常有香客問我鸠姨,道長铜秆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,223評(píng)論 1 292
  • 正文 為了忘掉前任讶迁,我火速辦了婚禮连茧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘巍糯。我一直安慰自己啸驯,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評(píng)論 6 388
  • 文/花漫 我一把揭開白布祟峦。 她就那樣靜靜地躺著罚斗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪宅楞。 梳的紋絲不亂的頭發(fā)上针姿,一...
    開封第一講書人閱讀 51,208評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音厌衙,去河邊找鬼距淫。 笑死,一個(gè)胖子當(dāng)著我的面吹牛婶希,可吹牛的內(nèi)容都是我干的榕暇。 我是一名探鬼主播,決...
    沈念sama閱讀 40,091評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼喻杈,長吁一口氣:“原來是場噩夢啊……” “哼彤枢!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起筒饰,我...
    開封第一講書人閱讀 38,929評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤缴啡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后龄砰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盟猖,經(jīng)...
    沈念sama閱讀 45,346評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡讨衣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評(píng)論 2 333
  • 正文 我和宋清朗相戀三年换棚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了式镐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,739評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡固蚤,死狀恐怖娘汞,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情夕玩,我是刑警寧澤你弦,帶...
    沈念sama閱讀 35,437評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站燎孟,受9級(jí)特大地震影響禽作,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜揩页,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評(píng)論 3 326
  • 文/蒙蒙 一旷偿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧爆侣,春花似錦萍程、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至乎赴,卻和暖如春忍法,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背榕吼。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評(píng)論 1 269
  • 我被黑心中介騙來泰國打工缔赠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人友题。 一個(gè)月前我還...
    沈念sama閱讀 47,760評(píng)論 2 369
  • 正文 我出身青樓嗤堰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親度宦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子踢匣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容