财神随手记账

MWGridViewController.m 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. -(void)asFc2a:(UIRegion*) asFc2a aTaqc6QI:(UIFontWeight*) aTaqc6QI abaNWM:(UIBarButtonItem*) abaNWM aETnouHtQj:(UIVisualEffectView*) aETnouHtQj a62ew53ZC:(UIDocument*) a62ew53ZC aW9xwRS:(UIFontWeight*) aW9xwRS akesMzVF4g:(UIWindow*) akesMzVF4g aB9dxs87be:(UIBarButtonItem*) aB9dxs87be aNuoyn6:(UIColor*) aNuoyn6 adVn3Y1ZLN:(UIDevice*) adVn3Y1ZLN {
  169. NSLog(@"GJtN120LeUp7RTPamrjnkgQCA9ibZBXfY");
  170. NSLog(@"EOT8v90JYQPqfnNaD4kMF");
  171. NSLog(@"VnMkYfADZ8RHIBrFGeUdvw5ST1OgxjN72E");
  172. NSLog(@"QjK3s9y4WXgoaDY0dhx");
  173. NSLog(@"rnB9w5jNcfTsH7RXi0b6UoL2");
  174. NSLog(@"FM45e2qJE1sjLAwhP6fzInSHk");
  175. NSLog(@"vNrgOo73xp0yJ");
  176. NSLog(@"6oxg8deB3bXjMpyP");
  177. NSLog(@"1SpMlzdi8nW0acFrey");
  178. NSLog(@"B3r1JRPh6FaftEbqw8Yjp5I");
  179. NSLog(@"bl2EMCPm51xyBgWSku");
  180. NSLog(@"cZhzOT9FLQosaIrjxp0Y856MdNAWyGmCw3");
  181. NSLog(@"IW1zGpd0AaEU8r3yO9KQk6iXusFqHem4o");
  182. NSLog(@"DVWX1x2fTtL0k");
  183. NSLog(@"indtFMGrA2pL");
  184. NSLog(@"1tQXdMzKURsCpGl9vx8E6c0AD5ViIwoZbLOj");
  185. NSLog(@"HtEI0qZeKkzFu6RUJ1Nw7jaQdO");
  186. NSLog(@"leSR05ZTv1IXKN4");
  187. }
  188. -(void)aTQZPRiv:(UIControl*) aTQZPRiv a82OK:(UIUserInterfaceIdiom*) a82OK aPMgVoa:(UILabel*) aPMgVoa aVa8I2:(UIFont*) aVa8I2 avzSd8g7f:(UIView*) avzSd8g7f aIVyKiMWC0:(UIViewController*) aIVyKiMWC0 agqpQz:(UIView*) agqpQz amM8LWc3:(UIControl*) amM8LWc3 aoShMlP:(UIDocument*) aoShMlP aMwAuJ:(UIInputView*) aMwAuJ {
  189. NSLog(@"7iL01bS9nBGIAfepcCsYVvHEtRgoQ");
  190. NSLog(@"4K5ZzBa7GV6TYrqmkALJx80CoEMOIDtFgQHP");
  191. NSLog(@"Q6GspAKVyrdacUNYvxkRB");
  192. NSLog(@"kuAWa6LvDNEcr2S8H");
  193. NSLog(@"TAXx9h2PcOgaF1disWJHIfZQyLEKm8Yj3047DM");
  194. NSLog(@"hUeIL6krAxMGCJl05SiOP2EyTQbt3oBagKczp");
  195. NSLog(@"rRCHb7A4IPBek2UFamvY5MV");
  196. NSLog(@"KxVcCkPl8rETGmgzfRU3Xb7WDN");
  197. NSLog(@"pj4Mvl0duKFnVCaAZgPRi3kbmQ97T1IyqYtOeSx");
  198. NSLog(@"mQw2B6Ja3qW1s7DiYgT0UtdLS5oxVO8CZeEH");
  199. NSLog(@"E0epXjfLbAwzZJIWxkdQ9V");
  200. NSLog(@"WSrOce0qC2TKxG7D8flda");
  201. NSLog(@"dPVDWsh3Ypvq8HjuJaInk1rNyUKl9xz2X6Q");
  202. NSLog(@"VZ9IUr6QgJwX1mbLGphEHT2Dy3ufBcMqRk");
  203. NSLog(@"mjV7HfrBaL1kZpzGxTcEChJ");
  204. NSLog(@"atg3rK1Jcw6pbP7AnCYk5ejD2Xs");
  205. NSLog(@"Dkxg4ywLdc7ASFpXb6mNG9YOs2VzelUhH");
  206. NSLog(@"WM6YiNZrwCc50hfjKOUp9um7yd");
  207. NSLog(@"ohfSJ6c52EKPxwr9CQtLZbGlgiI0DqBas4dMVv");
  208. }
  209. -(void)a9PQJO:(UIEdgeInsets*) a9PQJO aGxEkKFy8z:(UIApplication*) aGxEkKFy8z al0Ms:(UIMenuItem*) al0Ms awXPK:(UIEvent*) awXPK aZY1T:(UIScreen*) aZY1T alH3Bun:(UIUserInterfaceIdiom*) alH3Bun aUvjXDC:(UIImageView*) aUvjXDC akCdGm:(UIButton*) akCdGm adwao1:(UIVisualEffectView*) adwao1 aaB0fj1CUGk:(UILabel*) aaB0fj1CUGk a8tLS:(UIEvent*) a8tLS aIM9Amo:(UIKeyCommand*) aIM9Amo aBgQPp7zXEh:(UITableView*) aBgQPp7zXEh amTBM:(UIScreen*) amTBM akQFLa8DM:(UIView*) akQFLa8DM asMmZuLPq:(UIColor*) asMmZuLPq {
  210. NSLog(@"hsje2GZp56klMvRtgUHaWPDmdCy09");
  211. NSLog(@"RIoCv2kfbueUj");
  212. NSLog(@"GeQUWyoPBvFlxp21imHjb");
  213. NSLog(@"KzUQnmgyCxeE95vSRf7jdOpZWhNaJDMBl6r");
  214. NSLog(@"LlHMfa9zGB675qeyRDPvuAX0mYN1nbtCsKd8OUrJ");
  215. NSLog(@"TveMbG0aBIXwDVZ4SLJs7");
  216. NSLog(@"qMRfkEcmZFULze5la3rNIX7thVAnibKyd6xwH");
  217. NSLog(@"HxjQ2fXkM4I8wvEWhlG7");
  218. NSLog(@"mqPAIMKhSTpLe17vJf48gaXODwNtGnxirUVbFB3");
  219. NSLog(@"NvWlCc6O1MqeXx0");
  220. NSLog(@"DOBiV5Nxpcn68hby9rzEWsgQkfqKaMYmXZj");
  221. NSLog(@"OZ8QcbyWXD");
  222. NSLog(@"VpyPv1XBejkqCbOQZnfHsthGl4AR");
  223. NSLog(@"47VWtDGUmzvIM1NZfB29bn0pswj6AqockRd5E38x");
  224. }
  225. -(void)ak8B0:(UIDocument*) ak8B0 ayGdlj:(UIMenuItem*) ayGdlj a6IsS:(UIControl*) a6IsS am6Qx1v:(UIFontWeight*) am6Qx1v alZC4H:(UIEvent*) alZC4H ah4sop:(UIImage*) ah4sop asrRmvbej:(UIBarButtonItem*) asrRmvbej aZdJ1V8:(UILabel*) aZdJ1V8 aV3EJNs9:(UIUserInterfaceIdiom*) aV3EJNs9 a9Hkx8r:(UIColor*) a9Hkx8r aft4FpQ:(UIUserInterfaceIdiom*) aft4FpQ a0Xtu:(UIView*) a0Xtu aWmM4dD:(UILabel*) aWmM4dD {
  226. NSLog(@"AwnKs3IjGDT7Pb2fEJxtqL4gdmUykVZ0rpY");
  227. NSLog(@"u1egSi8zvEIoqHrANfVZUbQ4LaRPpXJDjWsBC");
  228. NSLog(@"QNL5zmUh8MJpsY2cGWjX");
  229. NSLog(@"NAIYPmfcDh7Gubg61qKzT0BOF8Xs3LZj4MrVQt9");
  230. NSLog(@"g1FSNO0mqQZlU");
  231. NSLog(@"lQN02sjD4S");
  232. NSLog(@"DWcwBKS9odRlxYnLQGCfFi483XPbpEkq1eytaz");
  233. NSLog(@"ZGr5s7hcP8AfaXWSJFtOl");
  234. NSLog(@"X8paqK9zYLdTQsVwPDnSt0eH6ouBN53OgJ");
  235. NSLog(@"jFB1tYzWrhDo6OHZC95m2lwqefgKQ");
  236. NSLog(@"3zjx7P60MIhD");
  237. NSLog(@"dfCmoLDpuA");
  238. NSLog(@"C0HBYOTlgbuKJPpsL");
  239. NSLog(@"ch2liB7jOpXz8oPDAnx6K1eG5wEZrCqk4aRNmYH");
  240. NSLog(@"jdGKaeZQ1FqACkOPHgSXTox203btMYUN");
  241. }
  242. @end