MASCompositeConstraint.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // MASCompositeConstraint.m
  3. // Masonry
  4. //
  5. // Created by Jonas Budelmann on 21/07/13.
  6. // Copyright (c) 2013 cloudling. All rights reserved.
  7. //
  8. #import "MASCompositeConstraint.h"
  9. #import "MASConstraint+Private.h"
  10. @interface MASCompositeConstraint () <MASConstraintDelegate>
  11. @property (nonatomic, strong) id mas_key;
  12. @property (nonatomic, strong) NSMutableArray *childConstraints;
  13. @end
  14. @implementation MASCompositeConstraint
  15. - (id)initWithChildren:(NSArray *)children {
  16. self = [super init];
  17. if (!self) return nil;
  18. _childConstraints = [children mutableCopy];
  19. for (MASConstraint *constraint in _childConstraints) {
  20. constraint.delegate = self;
  21. }
  22. return self;
  23. }
  24. #pragma mark - MASConstraintDelegate
  25. - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint {
  26. NSUInteger index = [self.childConstraints indexOfObject:constraint];
  27. NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint);
  28. [self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint];
  29. }
  30. - (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
  31. id<MASConstraintDelegate> strongDelegate = self.delegate;
  32. MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
  33. newConstraint.delegate = self;
  34. [self.childConstraints addObject:newConstraint];
  35. return newConstraint;
  36. }
  37. #pragma mark - NSLayoutConstraint multiplier proxies
  38. - (MASConstraint * (^)(CGFloat))multipliedBy {
  39. return ^id(CGFloat multiplier) {
  40. for (MASConstraint *constraint in self.childConstraints) {
  41. constraint.multipliedBy(multiplier);
  42. }
  43. return self;
  44. };
  45. }
  46. - (MASConstraint * (^)(CGFloat))dividedBy {
  47. return ^id(CGFloat divider) {
  48. for (MASConstraint *constraint in self.childConstraints) {
  49. constraint.dividedBy(divider);
  50. }
  51. return self;
  52. };
  53. }
  54. #pragma mark - MASLayoutPriority proxy
  55. - (MASConstraint * (^)(MASLayoutPriority))priority {
  56. return ^id(MASLayoutPriority priority) {
  57. for (MASConstraint *constraint in self.childConstraints) {
  58. constraint.priority(priority);
  59. }
  60. return self;
  61. };
  62. }
  63. #pragma mark - NSLayoutRelation proxy
  64. - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation {
  65. return ^id(id attr, NSLayoutRelation relation) {
  66. for (MASConstraint *constraint in self.childConstraints.copy) {
  67. constraint.equalToWithRelation(attr, relation);
  68. }
  69. return self;
  70. };
  71. }
  72. #pragma mark - attribute chaining
  73. - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
  74. [self constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
  75. return self;
  76. }
  77. #pragma mark - Animator proxy
  78. #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
  79. - (MASConstraint *)animator {
  80. for (MASConstraint *constraint in self.childConstraints) {
  81. [constraint animator];
  82. }
  83. return self;
  84. }
  85. #endif
  86. #pragma mark - debug helpers
  87. - (MASConstraint * (^)(id))key {
  88. return ^id(id key) {
  89. self.mas_key = key;
  90. int i = 0;
  91. for (MASConstraint *constraint in self.childConstraints) {
  92. constraint.key([NSString stringWithFormat:@"%@[%d]", key, i++]);
  93. }
  94. return self;
  95. };
  96. }
  97. #pragma mark - NSLayoutConstraint constant setters
  98. - (void)setInsets:(MASEdgeInsets)insets {
  99. for (MASConstraint *constraint in self.childConstraints) {
  100. constraint.insets = insets;
  101. }
  102. }
  103. - (void)setInset:(CGFloat)inset {
  104. for (MASConstraint *constraint in self.childConstraints) {
  105. constraint.inset = inset;
  106. }
  107. }
  108. - (void)setOffset:(CGFloat)offset {
  109. for (MASConstraint *constraint in self.childConstraints) {
  110. constraint.offset = offset;
  111. }
  112. }
  113. - (void)setSizeOffset:(CGSize)sizeOffset {
  114. for (MASConstraint *constraint in self.childConstraints) {
  115. constraint.sizeOffset = sizeOffset;
  116. }
  117. }
  118. - (void)setCenterOffset:(CGPoint)centerOffset {
  119. for (MASConstraint *constraint in self.childConstraints) {
  120. constraint.centerOffset = centerOffset;
  121. }
  122. }
  123. #pragma mark - MASConstraint
  124. - (void)activate {
  125. for (MASConstraint *constraint in self.childConstraints) {
  126. [constraint activate];
  127. }
  128. }
  129. - (void)deactivate {
  130. for (MASConstraint *constraint in self.childConstraints) {
  131. [constraint deactivate];
  132. }
  133. }
  134. - (void)install {
  135. for (MASConstraint *constraint in self.childConstraints) {
  136. constraint.updateExisting = self.updateExisting;
  137. [constraint install];
  138. }
  139. }
  140. - (void)uninstall {
  141. for (MASConstraint *constraint in self.childConstraints) {
  142. [constraint uninstall];
  143. }
  144. }
  145. @end