NSArray+MASAdditions.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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[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 == 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[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 == 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[i];
  90. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.width.equalTo(@(fixedItemLength));
  92. if (prev) {
  93. if (i == self.count - 1) {//last one
  94. make.right.equalTo(tempSuperView).offset(-tailSpacing);
  95. }
  96. else {
  97. CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1));
  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. }
  104. }];
  105. prev = v;
  106. }
  107. }
  108. else {
  109. MAS_VIEW *prev;
  110. for (int i = 0; i < self.count; i++) {
  111. MAS_VIEW *v = self[i];
  112. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.height.equalTo(@(fixedItemLength));
  114. if (prev) {
  115. if (i == self.count - 1) {//last one
  116. make.bottom.equalTo(tempSuperView).offset(-tailSpacing);
  117. }
  118. else {
  119. CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1));
  120. make.bottom.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset);
  121. }
  122. }
  123. else {//first one
  124. make.top.equalTo(tempSuperView).offset(leadSpacing);
  125. }
  126. }];
  127. prev = v;
  128. }
  129. }
  130. }
  131. - (MAS_VIEW *)mas_commonSuperviewOfViews
  132. {
  133. MAS_VIEW *commonSuperview = nil;
  134. MAS_VIEW *previousView = nil;
  135. for (id object in self) {
  136. if ([object isKindOfClass:[MAS_VIEW class]]) {
  137. MAS_VIEW *view = (MAS_VIEW *)object;
  138. if (previousView) {
  139. commonSuperview = [view mas_closestCommonSuperview:commonSuperview];
  140. } else {
  141. commonSuperview = view;
  142. }
  143. previousView = view;
  144. }
  145. }
  146. 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.");
  147. return commonSuperview;
  148. }
  149. @end