MWGridViewController.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // MWGridViewController.m
  3. // MWPhotoBrowser
  4. //
  5. // Created by Michael Waterfall on 08/10/2013.
  6. //
  7. //
  8. #import "MWGridViewController.h"
  9. #import "MWGridCell.h"
  10. #import "MWPhotoBrowserPrivate.h"
  11. #import "MWCommon.h"
  12. @interface MWGridViewController () {
  13. // Store margins for current setup
  14. CGFloat _margin, _gutter, _marginL, _gutterL, _columns, _columnsL;
  15. }
  16. @end
  17. @implementation MWGridViewController
  18. - (id)init {
  19. if ((self = [super initWithCollectionViewLayout:[UICollectionViewFlowLayout new]])) {
  20. // Defaults
  21. _columns = 3, _columnsL = 4;
  22. _margin = 0, _gutter = 1;
  23. _marginL = 0, _gutterL = 1;
  24. // For pixel perfection...
  25. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  26. // iPad
  27. _columns = 6, _columnsL = 8;
  28. _margin = 1, _gutter = 2;
  29. _marginL = 1, _gutterL = 2;
  30. } else if ([UIScreen mainScreen].bounds.size.height == 480) {
  31. // iPhone 3.5 inch
  32. _columns = 3, _columnsL = 4;
  33. _margin = 0, _gutter = 1;
  34. _marginL = 1, _gutterL = 2;
  35. } else {
  36. // iPhone 4 inch
  37. _columns = 3, _columnsL = 5;
  38. _margin = 0, _gutter = 1;
  39. _marginL = 0, _gutterL = 2;
  40. }
  41. _initialContentOffset = CGPointMake(0, CGFLOAT_MAX);
  42. }
  43. return self;
  44. }
  45. #pragma mark - View
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. [self.collectionView registerClass:[MWGridCell class] forCellWithReuseIdentifier:@"GridCell"];
  49. self.collectionView.alwaysBounceVertical = YES;
  50. self.collectionView.backgroundColor = [UIColor blackColor];
  51. }
  52. - (void)viewWillDisappear:(BOOL)animated {
  53. // Cancel outstanding loading
  54. NSArray *visibleCells = [self.collectionView visibleCells];
  55. if (visibleCells) {
  56. for (MWGridCell *cell in visibleCells) {
  57. [cell.photo cancelAnyLoading];
  58. }
  59. }
  60. [super viewWillDisappear:animated];
  61. }
  62. - (void)viewWillLayoutSubviews {
  63. [super viewWillLayoutSubviews];
  64. [self performLayout];
  65. }
  66. - (void)viewDidLayoutSubviews {
  67. [super viewDidLayoutSubviews];
  68. }
  69. - (void)adjustOffsetsAsRequired {
  70. // Move to previous content offset
  71. if (_initialContentOffset.y != CGFLOAT_MAX) {
  72. self.collectionView.contentOffset = _initialContentOffset;
  73. [self.collectionView layoutIfNeeded]; // Layout after content offset change
  74. }
  75. // Check if current item is visible and if not, make it so!
  76. if (_browser.numberOfPhotos > 0) {
  77. NSIndexPath *currentPhotoIndexPath = [NSIndexPath indexPathForItem:_browser.currentIndex inSection:0];
  78. NSArray *visibleIndexPaths = [self.collectionView indexPathsForVisibleItems];
  79. BOOL currentVisible = NO;
  80. for (NSIndexPath *indexPath in visibleIndexPaths) {
  81. if ([indexPath isEqual:currentPhotoIndexPath]) {
  82. currentVisible = YES;
  83. break;
  84. }
  85. }
  86. if (!currentVisible) {
  87. [self.collectionView scrollToItemAtIndexPath:currentPhotoIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  88. }
  89. }
  90. }
  91. - (void)performLayout {
  92. UINavigationBar *navBar = self.navigationController.navigationBar;
  93. self.collectionView.contentInset = UIEdgeInsetsMake(navBar.frame.origin.y + navBar.frame.size.height + [self getGutter], 0, 0, 0);
  94. }
  95. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  96. [self.collectionView reloadData];
  97. [self performLayout]; // needed for iOS 5 & 6
  98. }
  99. #pragma mark - Layout
  100. - (CGFloat)getColumns {
  101. if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) {
  102. return _columns;
  103. } else {
  104. return _columnsL;
  105. }
  106. }
  107. - (CGFloat)getMargin {
  108. if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) {
  109. return _margin;
  110. } else {
  111. return _marginL;
  112. }
  113. }
  114. - (CGFloat)getGutter {
  115. if ((UIInterfaceOrientationIsPortrait(self.interfaceOrientation))) {
  116. return _gutter;
  117. } else {
  118. return _gutterL;
  119. }
  120. }
  121. #pragma mark - Collection View
  122. - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
  123. return [_browser numberOfPhotos];
  124. }
  125. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  126. MWGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GridCell" forIndexPath:indexPath];
  127. if (!cell) {
  128. cell = [[MWGridCell alloc] init];
  129. }
  130. id <MWPhoto> photo = [_browser thumbPhotoAtIndex:indexPath.row];
  131. cell.photo = photo;
  132. cell.gridController = self;
  133. cell.selectionMode = _selectionMode;
  134. cell.isSelected = [_browser photoIsSelectedAtIndex:indexPath.row];
  135. cell.index = indexPath.row;
  136. UIImage *img = [_browser imageForPhoto:photo];
  137. if (img) {
  138. [cell displayImage];
  139. } else {
  140. [photo loadUnderlyingImageAndNotify];
  141. }
  142. return cell;
  143. }
  144. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  145. [_browser setCurrentPhotoIndex:indexPath.row];
  146. [_browser hideGrid];
  147. }
  148. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  149. [((MWGridCell *)cell).photo cancelAnyLoading];
  150. }
  151. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  152. CGFloat margin = [self getMargin];
  153. CGFloat gutter = [self getGutter];
  154. CGFloat columns = [self getColumns];
  155. CGFloat value = floorf(((self.view.bounds.size.width - (columns - 1) * gutter - 2 * margin) / columns));
  156. return CGSizeMake(value, value);
  157. }
  158. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  159. return [self getGutter];
  160. }
  161. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  162. return [self getGutter];
  163. }
  164. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  165. CGFloat margin = [self getMargin];
  166. return UIEdgeInsetsMake(margin, margin, margin, margin);
  167. }
  168. @end