CTAssetsGridViewLayout.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. MIT License (MIT)
  3. Copyright (c) 2015 Clement CN Tsang
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #import "CTAssetsGridViewLayout.h"
  21. @implementation CTAssetsGridViewLayout
  22. - (instancetype)initWithContentSize:(CGSize)contentSize traitCollection:(UITraitCollection *)traits
  23. {
  24. if (self = [super init])
  25. {
  26. CGFloat scale = traits.displayScale;
  27. NSInteger numberOfColumns = [self numberOfColumnsForTraitCollection:traits];
  28. CGFloat onePixel = (scale == 3.0) ? (2.0 / scale) : (1.0 / scale);
  29. // spacing is as small as possible
  30. self.minimumInteritemSpacing = onePixel;
  31. self.minimumLineSpacing = onePixel;
  32. // total spaces between items (in pixel)
  33. CGFloat spaces = self.minimumInteritemSpacing * (numberOfColumns - 1);
  34. // item length (in pixel)
  35. CGFloat length = (scale * (contentSize.width - spaces)) / numberOfColumns;
  36. // remaining spaces (in pixel) after rounding the length to integer
  37. CGFloat insets = (length - floor(length)) * numberOfColumns;
  38. // round the length to integer (in pixel)
  39. length = floor(length);
  40. // divide insets to two
  41. CGFloat left = insets / 2;
  42. CGFloat right = insets / 2;
  43. // adjust if insets is odd
  44. if (fmodf(insets, 2.0) == 1.0f)
  45. {
  46. left -= 0.5;
  47. right += 0.5;
  48. }
  49. // left / right insets (in point, 2 decimal)
  50. left = floorf(left / scale * 100) / 100;
  51. right = floorf(right / scale * 100) / 100;
  52. // item length (in point, 2 decimal)
  53. length = floorf(length / scale * 100) / 100;
  54. self.sectionInset = UIEdgeInsetsMake(0, left, 0, right);
  55. self.itemSize = CGSizeMake(length, length);
  56. self.footerReferenceSize = CGSizeMake(contentSize.width, floor(length * 2/3));
  57. }
  58. return self;
  59. }
  60. - (NSInteger)numberOfColumnsForTraitCollection:(UITraitCollection *)traits
  61. {
  62. switch (traits.userInterfaceIdiom) {
  63. case UIUserInterfaceIdiomPad:
  64. {
  65. return 6;
  66. break;
  67. }
  68. case UIUserInterfaceIdiomPhone:
  69. {
  70. // iPhone 6+ landscape
  71. if (traits.horizontalSizeClass == UIUserInterfaceSizeClassRegular)
  72. return 4;
  73. // iPhone landscape
  74. else if (traits.verticalSizeClass == UIUserInterfaceSizeClassCompact)
  75. return 6;
  76. // iPhone portrait
  77. else
  78. return 4;
  79. break;
  80. }
  81. default:
  82. return 4;
  83. break;
  84. }
  85. }
  86. @end