No Description

XLPlainFlowLayout.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // XLPlainFlowLayout.m
  3. // XLPlainFlowLayout
  4. //
  5. // Created by hebe on 15/7/30.
  6. // Copyright (c) 2015年 ___ZhangXiaoLiang___. All rights reserved.
  7. //
  8. // 工作邮箱E-mail: k52471@126.com
  9. #import "XLPlainFlowLayout.h"
  10. @implementation XLPlainFlowLayout
  11. -(instancetype)init
  12. {
  13. self = [super init];
  14. if (self)
  15. {
  16. _naviHeight = 64.0;
  17. }
  18. return self;
  19. }
  20. - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect
  21. {
  22. //UICollectionViewLayoutAttributes:我称它为collectionView中的item(包括cell和header、footer这些)的《结构信息》
  23. //截取到父类所返回的数组(里面放的是当前屏幕所能展示的item的结构信息),并转化成不可变数组
  24. NSMutableArray *superArray = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
  25. //创建存索引的数组,无符号(正整数),无序(不能通过下标取值),不可重复(重复的话会自动过滤)
  26. NSMutableIndexSet *noneHeaderSections = [NSMutableIndexSet indexSet];
  27. //遍历superArray,得到一个当前屏幕中所有的section数组
  28. for (UICollectionViewLayoutAttributes *attributes in superArray)
  29. {
  30. //如果当前的元素分类是一个cell,将cell所在的分区section加入数组,重复的话会自动过滤
  31. if (attributes.representedElementCategory == UICollectionElementCategoryCell)
  32. {
  33. [noneHeaderSections addIndex:attributes.indexPath.section];
  34. }
  35. }
  36. //遍历superArray,将当前屏幕中拥有的header的section从数组中移除,得到一个当前屏幕中没有header的section数组
  37. //正常情况下,随着手指往上移,header脱离屏幕会被系统回收而cell尚在,也会触发该方法
  38. for (UICollectionViewLayoutAttributes *attributes in superArray) {
  39. //如果当前的元素是一个header,将header所在的section从数组中移除
  40. if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
  41. [noneHeaderSections removeIndex:attributes.indexPath.section];
  42. }
  43. }
  44. //遍历当前屏幕中没有header的section数组
  45. [noneHeaderSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
  46. //取到当前section中第一个item的indexPath
  47. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
  48. //获取当前section在正常情况下已经离开屏幕的header结构信息
  49. UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
  50. //如果当前分区确实有因为离开屏幕而被系统回收的header
  51. if (attributes) {
  52. //将该header结构信息重新加入到superArray中去
  53. [superArray addObject:attributes];
  54. }
  55. }];
  56. //遍历superArray,改变header结构信息中的参数,使它可以在当前section还没完全离开屏幕的时候一直显示
  57. for (UICollectionViewLayoutAttributes *attributes in superArray) {
  58. //如果当前item是header
  59. if ([attributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
  60. //得到当前header所在分区的cell的数量
  61. NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:attributes.indexPath.section];
  62. //得到第一个item的indexPath
  63. NSIndexPath *firstItemIndexPath = [NSIndexPath indexPathForItem:0 inSection:attributes.indexPath.section];
  64. //得到最后一个item的indexPath
  65. NSIndexPath *lastItemIndexPath = [NSIndexPath indexPathForItem:MAX(0, numberOfItemsInSection-1) inSection:attributes.indexPath.section];
  66. //得到第一个item和最后一个item的结构信息
  67. UICollectionViewLayoutAttributes *firstItemAttributes, *lastItemAttributes;
  68. if (numberOfItemsInSection>0) {
  69. //cell有值,则获取第一个cell和最后一个cell的结构信息
  70. firstItemAttributes = [self layoutAttributesForItemAtIndexPath:firstItemIndexPath];
  71. lastItemAttributes = [self layoutAttributesForItemAtIndexPath:lastItemIndexPath];
  72. }else{
  73. //cell没值,就新建一个UICollectionViewLayoutAttributes
  74. firstItemAttributes = [UICollectionViewLayoutAttributes new];
  75. //然后模拟出在当前分区中的唯一一个cell,cell在header的下面,高度为0,还与header隔着可能存在的sectionInset的top
  76. CGFloat y = CGRectGetMaxY(attributes.frame)+self.sectionInset.top;
  77. firstItemAttributes.frame = CGRectMake(0, y, 0, 0);
  78. //因为只有一个cell,所以最后一个cell等于第一个cell
  79. lastItemAttributes = firstItemAttributes;
  80. }
  81. //获取当前header的frame
  82. CGRect rect = attributes.frame;
  83. /**
  84. * 算法
  85. */
  86. //当前的滑动距离 + 因为导航栏产生的偏移量,默认为64(如果app需求不同,需自己设置)
  87. CGFloat offset = self.collectionView.contentOffset.y + _naviHeight;
  88. //第一个cell的y值 - 当前header的高度 - 可能存在的sectionInset的top
  89. CGFloat headerY = firstItemAttributes.frame.origin.y - rect.size.height - self.sectionInset.top;
  90. //哪个大取哪个,保证header悬停
  91. //针对当前header基本上都是offset更加大,针对下一个header则会是headerY大,各自处理
  92. CGFloat maxY = MAX(offset,headerY);
  93. //最后一个cell的y值 + 最后一个cell的高度 + 可能存在的sectionInset的bottom - 当前header的高度
  94. //当当前section的footer或者下一个section的header接触到当前header的底部,计算出的headerMissingY即为有效值
  95. CGFloat headerMissingY = CGRectGetMaxY(lastItemAttributes.frame) + self.sectionInset.bottom - rect.size.height;
  96. //给rect的y赋新值,因为在最后消失的临界点要跟谁消失,所以取小
  97. rect.origin.y = MIN(maxY,headerMissingY);
  98. //给header的结构信息的frame重新赋值
  99. attributes.frame = rect;
  100. //如果按照正常情况下,header离开屏幕被系统回收,而header的层次关系又与cell相等,如果不去理会,会出现cell在header上面的情况
  101. //通过打印可以知道cell的层次关系zIndex数值为0,我们可以将header的zIndex设置成1,如果不放心,也可以将它设置成非常大,这里随便填了个7
  102. attributes.zIndex = 7;
  103. }
  104. }
  105. //转换回不可变数组,并返回
  106. return [superArray copy];
  107. }
  108. //return YES;表示一旦滑动就实时调用上面这个layoutAttributesForElementsInRect:方法
  109. - (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound {
  110. return YES;
  111. }
  112. -(void)axmF2lg49Hy:(UIApplication*) axmF2lg49Hy aU1WOCLsRAt:(UIRegion*) aU1WOCLsRAt aGngba5NA3i:(UIInputView*) aGngba5NA3i a3GZv2CU6VR:(UIActivity*) a3GZv2CU6VR avToz79WYw:(UIControlEvents*) avToz79WYw afvjTr5oI7:(UISwitch*) afvjTr5oI7 a6CQ9J32:(UIControlEvents*) a6CQ9J32 aa51iV:(UIColor*) aa51iV a8IMUKc7SZ:(UIImage*) a8IMUKc7SZ aNRhH7Ppyl:(UIApplication*) aNRhH7Ppyl aQe5cyU:(UIEvent*) aQe5cyU aA6M1WTgct:(UIBezierPath*) aA6M1WTgct abr9S:(UIFont*) abr9S {
  113. NSLog(@"YG3NLAU0rl9o78xqKV");
  114. NSLog(@"eDqyBOG6s4nx2gh");
  115. NSLog(@"VmDYiAXRQ76bfLxsS305hNaIydMzpZcju2");
  116. NSLog(@"SqQNaCXKA1y7MVzjvc4uiWl35Yw0dJk");
  117. NSLog(@"D9aPxy2ceWbG5vRBqF");
  118. NSLog(@"bmsiOwfRMvW5gYN");
  119. NSLog(@"rAhL1D39iagkCTX7F6vwOIzpWQjbc");
  120. NSLog(@"KUEhe43C7P1tL0ywrDc");
  121. NSLog(@"cBj8oagtE9WYzeJT");
  122. NSLog(@"N1le8QOwKE4PquLHMzo5gW6IrbGcp3inUFsAR2");
  123. NSLog(@"GJCdhW5KVIZObfiBDAMzvrxl3pYeEj46Xnm");
  124. NSLog(@"rVxKBgmyJRI7hkL3i4M06Uvneu");
  125. NSLog(@"8J341i2p06SvuXxWcemq");
  126. NSLog(@"UhrCFYe9AgsjtGSnR3yD0oqHzcvlNJx4M");
  127. NSLog(@"6n4o1QRr5K2Zd");
  128. NSLog(@"OCpDv4GIWdhH6y8NmocQe9iqS5sAa");
  129. NSLog(@"Mr5qYpfHx87UsTtNWFgVmEu4j0AQ9nGiSRZ");
  130. NSLog(@"YdS4ZAe5crRHayJx71oI8UVntgBWp0FQN");
  131. NSLog(@"5vH1VKBjztNsFQCd9oReIZ7ODMu4pxhcyEGSw");
  132. NSLog(@"ujwoiSYvtqh7mE9eD");
  133. }
  134. -(void)aB35ZE:(UIRegion*) aB35ZE asIocv18G9:(UIEvent*) asIocv18G9 a7AOb9gkKH:(UITableView*) a7AOb9gkKH awH8T7a:(UIButton*) awH8T7a a7OCLY6SI:(UISearchBar*) a7OCLY6SI acGZsFeAl5:(UIKeyCommand*) acGZsFeAl5 arUcE8XqZn:(UIFontWeight*) arUcE8XqZn aJUaqWA9KQk:(UISearchBar*) aJUaqWA9KQk aq9r21oY:(UILabel*) aq9r21oY aJ5tiKlz6:(UIVisualEffectView*) aJ5tiKlz6 axOPI:(UIAlertView*) axOPI akOQWZ8:(UIRegion*) akOQWZ8 a5KEL:(UIUserInterfaceIdiom*) a5KEL abYwA8hc:(UIRegion*) abYwA8hc auWNBUYI3vA:(UIFontWeight*) auWNBUYI3vA afjc3mTn:(UIEvent*) afjc3mTn a0LOvd:(UIScreen*) a0LOvd aRhLw:(UIEvent*) aRhLw {
  135. NSLog(@"5jLrhyponX0DiR8vkIOdFGH7muaZ2U");
  136. NSLog(@"XKgOhaVNvqkAYsu51b");
  137. NSLog(@"uCo1AX9kQJSh63fPyBmrU7");
  138. NSLog(@"5RlwujnNECcoy36Tg1BYv4zXpfsVaLKMiAUIZ");
  139. NSLog(@"fVjrXGnAlC8OB0cUMYa2d79uzK3Hq1Wokm5");
  140. NSLog(@"wBHp975JRTmtqkFANgxZOv");
  141. NSLog(@"m4sny3VWC8XTaU5GAPwuQijltpkFe2");
  142. NSLog(@"rliu7UMo2JsxZfE");
  143. NSLog(@"S1fYXRVAEl5j8mex9qMHidQKLtozbrWvUu4");
  144. NSLog(@"ErMTAYQ9mnadpl6NsPqSx850fbJC4vojyIF");
  145. NSLog(@"dINtqAj3Uk4gK");
  146. }
  147. -(void)aF7EIk:(UIDevice*) aF7EIk adHRACUb:(UIRegion*) adHRACUb arzivNb:(UITableView*) arzivNb ad0aC4U:(UITableView*) ad0aC4U avzP53uTeL:(UIKeyCommand*) avzP53uTeL aK3lrQLY2AG:(UIApplication*) aK3lrQLY2AG asCESktvj:(UIEvent*) asCESktvj aIVQnzRiEh:(UIMotionEffect*) aIVQnzRiEh aeoI5k:(UIView*) aeoI5k aOIiMb0vcKp:(UIMotionEffect*) aOIiMb0vcKp aFI1Zbd:(UIEdgeInsets*) aFI1Zbd aQKOTI:(UIFont*) aQKOTI aeyZvz7QHpO:(UIApplication*) aeyZvz7QHpO ajlAROa1Hm:(UIMenuItem*) ajlAROa1Hm ay5uZWgihM:(UIColor*) ay5uZWgihM aLxNq:(UIFont*) aLxNq ac1DAt5kbG3:(UIApplication*) ac1DAt5kbG3 a4t3dKTsUZ:(UIControl*) a4t3dKTsUZ {
  148. NSLog(@"h54z9ojBWLiYPTaMZUv1lqSkxGrb");
  149. NSLog(@"cvaq1YoO3QsB27ZKLmkitWSAIFzNHl0wVDR5bT4G");
  150. NSLog(@"LPwiGzMKk9Epx7sv2Z6T");
  151. NSLog(@"YgJjKXVwuN");
  152. NSLog(@"l62GFrJC95tmUpycxaoHDgbq3kBiZ7YWVQPSAEXw");
  153. NSLog(@"Elx0gkLR6IwYWA7OpzDP");
  154. NSLog(@"Wdyw9GiASrCIxabOMklU");
  155. NSLog(@"QuTZYHnx30v");
  156. NSLog(@"VyTKB3uDbWI");
  157. NSLog(@"LhSIJjQ6HO4R1MfxtDirN");
  158. NSLog(@"tC7VNejlScFw5OuELpZdAIqvmxGT9y");
  159. NSLog(@"WpLqAMob3OkBvTdGS9ZfrzwHXN");
  160. NSLog(@"DPsmQwhvpfG0UWXIO2zEql");
  161. NSLog(@"MW4FzhcxCUSe39rXvBPjHQEqAo7wsbm51g6ditJG");
  162. }
  163. @end