RGCardViewLayout.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // RGCardViewLayout.m
  3. // RGCardViewLayout
  4. //
  5. // Created by ROBERA GELETA on 1/23/15.
  6. // Copyright (c) 2015 ROBERA GELETA. All rights reserved.
  7. //
  8. #import "RGCardViewLayout.h"
  9. @implementation RGCardViewLayout
  10. {
  11. CGFloat previousOffset;
  12. NSIndexPath *mainIndexPath;
  13. NSIndexPath *movingInIndexPath;
  14. CGFloat diffrence;
  15. }
  16. - (void)prepareLayout
  17. {
  18. [super prepareLayout];
  19. [self setupLayout];
  20. }
  21. - (void)setupLayout
  22. {
  23. //danson 好流弊 这样的话 点进去的就会是当前点击的图标 测试没问题 当初虐死自己的地方 现在依然虐了自己好久 还好搞定了
  24. if (_isCan == YES) {
  25. _isCan = NO;
  26. mainIndexPath = [NSIndexPath indexPathForRow:0 inSection:myDelegate.layoutIndex];
  27. [self.collectionView scrollToItemAtIndexPath:mainIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
  28. }
  29. CGFloat inset = self.collectionView.bounds.size.width * (6/64.0f);
  30. inset = floor(inset);
  31. self.itemSize = CGSizeMake(self.collectionView.bounds.size.width - (2 *inset), self.collectionView.bounds.size.height * 3/4);
  32. self.sectionInset = UIEdgeInsetsMake(0,inset, 0,inset);
  33. self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  34. }
  35. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
  36. {
  37. UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
  38. [self applyTransformToLayoutAttributes:attributes];
  39. return attributes;
  40. }
  41. // indicate that we want to redraw as we scroll
  42. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  43. return YES;
  44. }
  45. -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
  46. {
  47. NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
  48. NSArray *cellIndices = [self.collectionView indexPathsForVisibleItems];
  49. if(cellIndices.count == 0 )
  50. {
  51. return attributes;
  52. }
  53. else if (cellIndices.count == 1)
  54. {
  55. mainIndexPath = cellIndices.firstObject;
  56. movingInIndexPath = nil;
  57. }else if(cellIndices.count > 1)
  58. {
  59. NSIndexPath *firstIndexPath = cellIndices.firstObject;
  60. if(firstIndexPath == mainIndexPath)
  61. {
  62. movingInIndexPath = cellIndices[1];
  63. }
  64. else
  65. {
  66. movingInIndexPath = cellIndices.firstObject;
  67. mainIndexPath = cellIndices[1];
  68. }
  69. }
  70. diffrence = self.collectionView.contentOffset.x - previousOffset;
  71. previousOffset = self.collectionView.contentOffset.x;
  72. for (UICollectionViewLayoutAttributes *attribute in attributes)
  73. {
  74. [self applyTransformToLayoutAttributes:attribute];
  75. }
  76. return attributes;
  77. }
  78. - (void)applyTransformToLayoutAttributes:(UICollectionViewLayoutAttributes *)attribute
  79. {
  80. if(attribute.indexPath.section == mainIndexPath.section)
  81. {
  82. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:mainIndexPath];
  83. attribute.transform3D = [self transformFromView:cell];
  84. }
  85. else if (attribute.indexPath.section == movingInIndexPath.section)
  86. {
  87. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:movingInIndexPath];
  88. attribute.transform3D = [self transformFromView:cell];
  89. }
  90. }
  91. - (CGRect)newFrameFromOriginal:(CGRect)orginalFrame withView:(UIView *)view
  92. {
  93. [self heightOffsetForView:view];
  94. return orginalFrame;
  95. }
  96. #pragma mark - Logica
  97. - (CGFloat)baseOffsetForView:(UIView *)view
  98. {
  99. UICollectionViewCell *cell = (UICollectionViewCell *)view;
  100. CGFloat offset = ([self.collectionView indexPathForCell:cell].section) * self.collectionView.bounds.size.width;
  101. return offset;
  102. }
  103. - (CGFloat)heightOffsetForView:(UIView *)view
  104. {
  105. CGFloat height;
  106. CGFloat baseOffsetForCurrentView = [self baseOffsetForView:view ];
  107. CGFloat currentOffset = self.collectionView.contentOffset.x;
  108. CGFloat scrollViewWidth = self.collectionView.bounds.size.width;
  109. //TODO:make this constant a certain proportion of the collection view
  110. height = 120 * (currentOffset - baseOffsetForCurrentView)/scrollViewWidth;
  111. if(height < 0 )
  112. {
  113. height = - 1 *height;
  114. }
  115. return height;
  116. }
  117. - (CGFloat)angleForView:(UIView *)view
  118. {
  119. CGFloat baseOffsetForCurrentView = [self baseOffsetForView:view ];
  120. CGFloat currentOffset = self.collectionView.contentOffset.x;
  121. CGFloat scrollViewWidth = self.collectionView.bounds.size.width;
  122. CGFloat angle = (currentOffset - baseOffsetForCurrentView)/scrollViewWidth;
  123. return angle;
  124. }
  125. - (BOOL)xAxisForView:(UIView *)view
  126. {
  127. CGFloat baseOffsetForCurrentView = [self baseOffsetForView:view ];
  128. CGFloat currentOffset = self.collectionView.contentOffset.x;
  129. CGFloat offset = (currentOffset - baseOffsetForCurrentView);
  130. if(offset >= 0)
  131. {
  132. return YES;
  133. }
  134. return NO;
  135. }
  136. #pragma mark - Transform Related Calculation
  137. - (CATransform3D)transformFromView:(UIView *)view
  138. {
  139. CGFloat angle = [self angleForView:view];
  140. CGFloat height = [self heightOffsetForView:view];
  141. BOOL xAxis = [self xAxisForView:view];
  142. return [self transformfromAngle:angle height:height xAxis:xAxis];
  143. }
  144. - (CATransform3D)transformfromAngle:(CGFloat )angle height:(CGFloat) height xAxis:(BOOL)axis
  145. {
  146. CATransform3D t = CATransform3DIdentity;
  147. t.m34 = 1.0/-500;
  148. if (axis)
  149. {
  150. t = CATransform3DRotate(t,angle, 1, 1, 0);
  151. }
  152. else
  153. {
  154. t = CATransform3DRotate(t,angle, -1, 1, 0);
  155. }
  156. // t = CATransform3DTranslate(t, 0, height, 0);
  157. return t;
  158. }
  159. @end
  160. // 发布代码于最专业的源码分享网站: Code4App.com