123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //
- // MASCompositeConstraint.m
- // Masonry
- //
- // Created by Jonas Budelmann on 21/07/13.
- // Copyright (c) 2013 cloudling. All rights reserved.
- //
- #import "MASCompositeConstraint.h"
- #import "MASConstraint+Private.h"
- @interface MASCompositeConstraint () <MASConstraintDelegate>
- @property (nonatomic, strong) id mas_key;
- @property (nonatomic, strong) NSMutableArray *childConstraints;
- @end
- @implementation MASCompositeConstraint
- - (id)initWithChildren:(NSArray *)children {
- self = [super init];
- if (!self) return nil;
- _childConstraints = [children mutableCopy];
- for (MASConstraint *constraint in _childConstraints) {
- constraint.delegate = self;
- }
- return self;
- }
- #pragma mark - MASConstraintDelegate
- - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint {
- NSUInteger index = [self.childConstraints indexOfObject:constraint];
- NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint);
- [self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint];
- }
- - (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
- id<MASConstraintDelegate> strongDelegate = self.delegate;
- MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
- newConstraint.delegate = self;
- [self.childConstraints addObject:newConstraint];
- return newConstraint;
- }
- #pragma mark - NSLayoutConstraint multiplier proxies
- - (MASConstraint * (^)(CGFloat))multipliedBy {
- return ^id(CGFloat multiplier) {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.multipliedBy(multiplier);
- }
- return self;
- };
- }
- - (MASConstraint * (^)(CGFloat))dividedBy {
- return ^id(CGFloat divider) {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.dividedBy(divider);
- }
- return self;
- };
- }
- #pragma mark - MASLayoutPriority proxy
- - (MASConstraint * (^)(MASLayoutPriority))priority {
- return ^id(MASLayoutPriority priority) {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.priority(priority);
- }
- return self;
- };
- }
- #pragma mark - NSLayoutRelation proxy
- - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation {
- return ^id(id attr, NSLayoutRelation relation) {
- for (MASConstraint *constraint in self.childConstraints.copy) {
- constraint.equalToWithRelation(attr, relation);
- }
- return self;
- };
- }
- #pragma mark - attribute chaining
- - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute {
- [self constraint:self addConstraintWithLayoutAttribute:layoutAttribute];
- return self;
- }
- #pragma mark - Animator proxy
- #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
- - (MASConstraint *)animator {
- for (MASConstraint *constraint in self.childConstraints) {
- [constraint animator];
- }
- return self;
- }
- #endif
- #pragma mark - debug helpers
- - (MASConstraint * (^)(id))key {
- return ^id(id key) {
- self.mas_key = key;
- int i = 0;
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.key([NSString stringWithFormat:@"%@[%d]", key, i++]);
- }
- return self;
- };
- }
- #pragma mark - NSLayoutConstraint constant setters
- - (void)setInsets:(MASEdgeInsets)insets {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.insets = insets;
- }
- }
- - (void)setInset:(CGFloat)inset {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.inset = inset;
- }
- }
- - (void)setOffset:(CGFloat)offset {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.offset = offset;
- }
- }
- - (void)setSizeOffset:(CGSize)sizeOffset {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.sizeOffset = sizeOffset;
- }
- }
- - (void)setCenterOffset:(CGPoint)centerOffset {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.centerOffset = centerOffset;
- }
- }
- #pragma mark - MASConstraint
- - (void)activate {
- for (MASConstraint *constraint in self.childConstraints) {
- [constraint activate];
- }
- }
- - (void)deactivate {
- for (MASConstraint *constraint in self.childConstraints) {
- [constraint deactivate];
- }
- }
- - (void)install {
- for (MASConstraint *constraint in self.childConstraints) {
- constraint.updateExisting = self.updateExisting;
- [constraint install];
- }
- }
- - (void)uninstall {
- for (MASConstraint *constraint in self.childConstraints) {
- [constraint uninstall];
- }
- }
- @end
|