WMZBannerFadeLayout.m 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // WMZBannerFadeLayout.m
  3. // WMZBanner
  4. //
  5. // Created by wmz on 2020/6/15.
  6. // Copyright © 2020 wmz. All rights reserved.
  7. //
  8. #import "WMZBannerFadeLayout.h"
  9. @interface WMZBannerFadeLayout()
  10. @property(nonatomic,assign)CGPoint collectionContenOffset;
  11. @property(nonatomic,assign)CGSize collectionContenSize;
  12. @property(nonatomic,assign)CGFloat last;
  13. @end
  14. @implementation WMZBannerFadeLayout
  15. - (instancetype)initConfigureWithModel:(WMZBannerParam *)param{
  16. if (self = [super init]) {
  17. self.param = param;
  18. }
  19. return self;
  20. }
  21. - (void)prepareLayout
  22. {
  23. [super prepareLayout];
  24. self.collectionView.bounces = NO;
  25. self.collectionView.pagingEnabled = YES;
  26. self.itemSize = self.param.wItemSize;
  27. self.minimumInteritemSpacing = (self.param.wFrame.size.height-self.param.wItemSize.height)/2;
  28. self.minimumLineSpacing = self.param.wLineSpacing;
  29. self.sectionInset = self.param.wSectionInset;
  30. self.scrollDirection = self.param.wVertical? UICollectionViewScrollDirectionVertical
  31. :UICollectionViewScrollDirectionHorizontal;
  32. }
  33. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  34. return [self cardOverLapTypeInRect:rect];
  35. }
  36. //卡片重叠
  37. - (NSArray<UICollectionViewLayoutAttributes *> *)cardOverLapTypeInRect:(CGRect)rect{
  38. NSInteger itemsCount = [self.collectionView numberOfItemsInSection:0];
  39. if (itemsCount <= 0) {
  40. return nil;
  41. }
  42. NSMutableArray *mArr = [[NSMutableArray alloc] init];
  43. if (self.param.wVertical) {
  44. if (self.collectionView.contentOffset.y>self.last) {
  45. self.right = YES;
  46. }else if (self.collectionView.contentOffset.y<self.last){
  47. self.right = NO;
  48. }
  49. }else{
  50. if (self.collectionView.contentOffset.x>self.last) {
  51. self.right = YES;
  52. }else if (self.collectionView.contentOffset.x<self.last){
  53. self.right = NO;
  54. }
  55. }
  56. self.param.myCurrentPath = self.param.wVertical?
  57. (self.right?MAX(floor(self.collectionContenOffset.y / (int)self.collectionContenSize.height), 0):MAX(ceil(self.collectionContenOffset.y / (int)self.collectionContenSize.height), 0)):
  58. (self.right?MAX(floor(self.collectionContenOffset.x / (int)self.collectionContenSize.width), 0):MAX(ceil(self.collectionContenOffset.x / (int)self.collectionContenSize.width), 0));
  59. NSInteger minVisibleIndex = MAX(self.param.myCurrentPath-1, 0);
  60. NSInteger maxVisibleIndex = MIN(self.param.myCurrentPath+1, itemsCount-1);
  61. for (NSInteger i = minVisibleIndex; i <= maxVisibleIndex; i++) {
  62. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  63. UICollectionViewLayoutAttributes *attributes = [[self layoutAttributesForItemAtIndexPath:indexPath] copy];
  64. CGRect rect = attributes.frame;
  65. if (self.param.wVertical) {
  66. rect.origin.y = self.collectionView.contentOffset.y;
  67. }else{
  68. rect.origin.x = self.collectionView.contentOffset.x;
  69. }
  70. attributes.frame = rect;
  71. if (i == self.param.myCurrentPath) {
  72. attributes.zIndex = 1200;
  73. }else{
  74. attributes.zIndex = 999-i;
  75. }
  76. [mArr addObject:attributes];
  77. }
  78. self.last = self.param.wVertical?self.collectionView.contentOffset.y:self.collectionView.contentOffset.x;
  79. return mArr;
  80. }
  81. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  82. return YES;
  83. }
  84. - (CGSize)collectionContenSize{
  85. return CGSizeMake((int)self.collectionView.bounds.size.width, (int)self.collectionView.bounds.size.height);
  86. }
  87. - (CGPoint)collectionContenOffset{
  88. return CGPointMake((int)self.collectionView.contentOffset.x, (int)self.collectionView.contentOffset.y);
  89. }
  90. @end