NSArray+MASAdditions.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // NSArray+MASAdditions.m
  3. //
  4. //
  5. // Created by Daniel Hammond on 11/26/13.
  6. //
  7. //
  8. #import "NSArray+MASAdditions.h"
  9. #import "View+MASAdditions.h"
  10. @implementation NSArray (MASAdditions)
  11. - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block {
  12. NSMutableArray *constraints = [NSMutableArray array];
  13. for (MAS_VIEW *view in self) {
  14. NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views");
  15. [constraints addObjectsFromArray:[view mas_makeConstraints:block]];
  16. }
  17. return constraints;
  18. }
  19. - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block {
  20. NSMutableArray *constraints = [NSMutableArray array];
  21. for (MAS_VIEW *view in self) {
  22. NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views");
  23. [constraints addObjectsFromArray:[view mas_updateConstraints:block]];
  24. }
  25. return constraints;
  26. }
  27. - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block {
  28. NSMutableArray *constraints = [NSMutableArray array];
  29. for (MAS_VIEW *view in self) {
  30. NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views");
  31. [constraints addObjectsFromArray:[view mas_remakeConstraints:block]];
  32. }
  33. return constraints;
  34. }
  35. - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing {
  36. if (self.count < 2) {
  37. NSAssert(self.count>1,@"views to distribute need to bigger than one");
  38. return;
  39. }
  40. MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews];
  41. if (axisType == MASAxisTypeHorizontal) {
  42. MAS_VIEW *prev;
  43. for (int i = 0; i < self.count; i++) {
  44. MAS_VIEW *v = [self objectAtIndex:i];
  45. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  46. if (prev) {
  47. make.width.equalTo(prev);
  48. make.left.equalTo(prev.mas_right).offset(fixedSpacing);
  49. if (i == (CGFloat)self.count - 1) {//last one
  50. make.right.equalTo(tempSuperView).offset(-tailSpacing);
  51. }
  52. }
  53. else {//first one
  54. make.left.equalTo(tempSuperView).offset(leadSpacing);
  55. }
  56. }];
  57. prev = v;
  58. }
  59. }
  60. else {
  61. MAS_VIEW *prev;
  62. for (int i = 0; i < self.count; i++) {
  63. MAS_VIEW *v = [self objectAtIndex:i];
  64. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  65. if (prev) {
  66. make.height.equalTo(prev);
  67. make.top.equalTo(prev.mas_bottom).offset(fixedSpacing);
  68. if (i == (CGFloat)self.count - 1) {//last one
  69. make.bottom.equalTo(tempSuperView).offset(-tailSpacing);
  70. }
  71. }
  72. else {//first one
  73. make.top.equalTo(tempSuperView).offset(leadSpacing);
  74. }
  75. }];
  76. prev = v;
  77. }
  78. }
  79. }
  80. - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing {
  81. if (self.count < 2) {
  82. NSAssert(self.count>1,@"views to distribute need to bigger than one");
  83. return;
  84. }
  85. MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews];
  86. if (axisType == MASAxisTypeHorizontal) {
  87. MAS_VIEW *prev;
  88. for (int i = 0; i < self.count; i++) {
  89. MAS_VIEW *v = [self objectAtIndex:i];
  90. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  91. if (prev) {
  92. CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1));
  93. make.width.equalTo(@(fixedItemLength));
  94. if (i == (CGFloat)self.count - 1) {//last one
  95. make.right.equalTo(tempSuperView).offset(-tailSpacing);
  96. }
  97. else {
  98. make.right.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset);
  99. }
  100. }
  101. else {//first one
  102. make.left.equalTo(tempSuperView).offset(leadSpacing);
  103. make.width.equalTo(@(fixedItemLength));
  104. }
  105. }];
  106. prev = v;
  107. }
  108. }
  109. else {
  110. MAS_VIEW *prev;
  111. for (int i = 0; i < self.count; i++) {
  112. MAS_VIEW *v = [self objectAtIndex:i];
  113. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  114. if (prev) {
  115. CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1));
  116. make.height.equalTo(@(fixedItemLength));
  117. if (i == (CGFloat)self.count - 1) {//last one
  118. make.bottom.equalTo(tempSuperView).offset(-tailSpacing);
  119. }
  120. else {
  121. make.bottom.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset);
  122. }
  123. }
  124. else {//first one
  125. make.top.equalTo(tempSuperView).offset(leadSpacing);
  126. make.height.equalTo(@(fixedItemLength));
  127. }
  128. }];
  129. prev = v;
  130. }
  131. }
  132. }
  133. - (MAS_VIEW *)mas_commonSuperviewOfViews
  134. {
  135. MAS_VIEW *commonSuperview = nil;
  136. MAS_VIEW *previousView = nil;
  137. for (id object in self) {
  138. if ([object isKindOfClass:[MAS_VIEW class]]) {
  139. MAS_VIEW *view = (MAS_VIEW *)object;
  140. if (previousView) {
  141. commonSuperview = [view mas_closestCommonSuperview:commonSuperview];
  142. } else {
  143. commonSuperview = view;
  144. }
  145. previousView = view;
  146. }
  147. }
  148. NSAssert(commonSuperview, @"Can't constrain views that do not share a common superview. Make sure that all the views in this array have been added into the same view hierarchy.");
  149. return commonSuperview;
  150. }
  151. @end