Skip to content

Commit

Permalink
Converted <MASConstraint> protocol to abstract class (to support prop…
Browse files Browse the repository at this point in the history
…er code completion in Xcode)
  • Loading branch information
nickynick committed Jan 20, 2014
1 parent eac9ac8 commit 455f887
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Examples/Masonry iOS Examples/MASExampleAnimatedView.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ - (void)animateWithInvertedInsets:(BOOL)invertedInsets {

int padding = invertedInsets ? 100 : self.padding;
UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
for (id<MASConstraint> constraint in self.animatableConstraints) {
for (MASConstraint *constraint in self.animatableConstraints) {
constraint.insets = paddingInsets;
}

Expand Down
3 changes: 3 additions & 0 deletions Masonry.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Masonry/MASCompositeConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

/**
* A group of MASConstraint objects
* conforms to MASConstraint
*/
@interface MASCompositeConstraint : NSObject <MASConstraint>
@interface MASCompositeConstraint : MASConstraint

/**
* Creates a composite with a predefined array of children
Expand Down
67 changes: 32 additions & 35 deletions Masonry/MASCompositeConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ @interface MASCompositeConstraint () <MASConstraintDelegate>

@implementation MASCompositeConstraint

@synthesize delegate = _delegate;
@synthesize updateExisting = _updateExisting;

- (id)initWithChildren:(NSArray *)children {
self = [super init];
if (!self) return nil;

_childConstraints = [children mutableCopy];
for (id<MASConstraint> constraint in _childConstraints) {
for (MASConstraint *constraint in _childConstraints) {
constraint.delegate = self;
}

Expand All @@ -34,36 +31,36 @@ - (id)initWithChildren:(NSArray *)children {

#pragma mark - MASConstraintDelegate

- (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:(id<MASConstraint>)replacementConstraint {
- (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];
}

#pragma mark - NSLayoutConstraint constant proxies

- (id<MASConstraint> (^)(MASEdgeInsets))insets {
- (MASConstraint * (^)(MASEdgeInsets))insets {
return ^id(MASEdgeInsets insets) {
self.insets = insets;
return self;
};
}

- (id<MASConstraint> (^)(CGFloat))offset {
- (MASConstraint * (^)(CGFloat))offset {
return ^id(CGFloat offset) {
self.offset = offset;
return self;
};
}

- (id<MASConstraint> (^)(CGSize))sizeOffset {
- (MASConstraint * (^)(CGSize))sizeOffset {
return ^id(CGSize offset) {
self.sizeOffset = offset;
return self;
};
}

- (id<MASConstraint> (^)(CGPoint))centerOffset {
- (MASConstraint * (^)(CGPoint))centerOffset {
return ^id(CGPoint offset) {
self.centerOffset = offset;
return self;
Expand All @@ -72,18 +69,18 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:

#pragma mark - NSLayoutConstraint multiplier proxies

- (id<MASConstraint> (^)(CGFloat))multipliedBy {
- (MASConstraint * (^)(CGFloat))multipliedBy {
return ^id(CGFloat multiplier) {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.multipliedBy(multiplier);
}
return self;
};
}

- (id<MASConstraint> (^)(CGFloat))dividedBy {
- (MASConstraint * (^)(CGFloat))dividedBy {
return ^id(CGFloat divider) {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.dividedBy(divider);
}
return self;
Expand All @@ -92,30 +89,30 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:

#pragma mark - MASLayoutPriority proxies

- (id<MASConstraint> (^)(MASLayoutPriority))priority {
- (MASConstraint * (^)(MASLayoutPriority))priority {
return ^id(MASLayoutPriority priority) {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.priority(priority);
}
return self;
};
}

- (id<MASConstraint> (^)())priorityLow {
- (MASConstraint * (^)())priorityLow {
return ^id{
self.priority(MASLayoutPriorityDefaultLow);
return self;
};
}

- (id<MASConstraint> (^)())priorityMedium {
- (MASConstraint * (^)())priorityMedium {
return ^id{
self.priority(MASLayoutPriorityDefaultMedium);
return self;
};
}

- (id<MASConstraint> (^)())priorityHigh {
- (MASConstraint * (^)())priorityHigh {
return ^id{
self.priority(MASLayoutPriorityDefaultHigh);
return self;
Expand All @@ -124,27 +121,27 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:

#pragma mark - NSLayoutRelation proxies

- (id<MASConstraint> (^)(id))equalTo {
- (MASConstraint * (^)(id))equalTo {
return ^id(id attr) {
for (id<MASConstraint> constraint in self.childConstraints.copy) {
for (MASConstraint *constraint in self.childConstraints.copy) {
constraint.equalTo(attr);
}
return self;
};
}

- (id<MASConstraint> (^)(id))greaterThanOrEqualTo {
- (MASConstraint * (^)(id))greaterThanOrEqualTo {
return ^id(id attr) {
for (id<MASConstraint> constraint in self.childConstraints.copy) {
for (MASConstraint *constraint in self.childConstraints.copy) {
constraint.greaterThanOrEqualTo(attr);
}
return self;
};
}

- (id<MASConstraint> (^)(id))lessThanOrEqualTo {
- (MASConstraint * (^)(id))lessThanOrEqualTo {
return ^id(id attr) {
for (id<MASConstraint> constraint in self.childConstraints.copy) {
for (MASConstraint *constraint in self.childConstraints.copy) {
constraint.lessThanOrEqualTo(attr);
}
return self;
Expand All @@ -153,16 +150,16 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:

#pragma mark - Semantic properties

- (id<MASConstraint>)with {
- (MASConstraint *)with {
return self;
}

#pragma mark - Animator proxy

#if TARGET_OS_MAC && !TARGET_OS_IPHONE

- (id<MASConstraint>)animator {
for (id<MASConstraint> constraint in self.childConstraints) {
- (MASConstraint *)animator {
for (MASConstraint *constraint in self.childConstraints) {
[constraint animator];
}
return self;
Expand All @@ -172,11 +169,11 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:

#pragma mark - debug helpers

- (id<MASConstraint> (^)(id))key {
- (MASConstraint * (^)(id))key {
return ^id(id key) {
self.mas_key = key;
int i = 0;
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.key([NSString stringWithFormat:@"%@[%d]", key, i++]);
}
return self;
Expand All @@ -186,40 +183,40 @@ - (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:
#pragma mark - NSLayoutConstraint constant setters

- (void)setInsets:(MASEdgeInsets)insets {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.insets = insets;
}
}

- (void)setOffset:(CGFloat)offset {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.offset = offset;
}
}

- (void)setSizeOffset:(CGSize)sizeOffset {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.sizeOffset = sizeOffset;
}
}

- (void)setCenterOffset:(CGPoint)centerOffset {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.centerOffset = centerOffset;
}
}

#pragma mark - MASConstraint

- (void)install {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
constraint.updateExisting = self.updateExisting;
[constraint install];
}
}

- (void)uninstall {
for (id<MASConstraint> constraint in self.childConstraints) {
for (MASConstraint *constraint in self.childConstraints) {
[constraint uninstall];
}
}
Expand Down
36 changes: 18 additions & 18 deletions Masonry/MASConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Constraint can represent single NSLayoutConstraint (MASViewConstraint)
* or a group of NSLayoutConstraints (MASComposisteConstraint)
*/
@protocol MASConstraint <NSObject>
@interface MASConstraint : NSObject

// Chaining Support

Expand All @@ -24,90 +24,90 @@
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
*/
- (id<MASConstraint> (^)(MASEdgeInsets insets))insets;
- (MASConstraint * (^)(MASEdgeInsets insets))insets;

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeWidth, NSLayoutAttributeHeight
*/
- (id<MASConstraint> (^)(CGSize offset))sizeOffset;
- (MASConstraint * (^)(CGSize offset))sizeOffset;

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
*/
- (id<MASConstraint> (^)(CGPoint offset))centerOffset;
- (MASConstraint * (^)(CGPoint offset))centerOffset;

/**
* Modifies the NSLayoutConstraint constant
*/
- (id<MASConstraint> (^)(CGFloat offset))offset;
- (MASConstraint * (^)(CGFloat offset))offset;

/**
* Sets the NSLayoutConstraint multiplier property
*/
- (id<MASConstraint> (^)(CGFloat multiplier))multipliedBy;
- (MASConstraint * (^)(CGFloat multiplier))multipliedBy;

/**
* Sets the NSLayoutConstraint multiplier to 1.0/dividedBy
*/
- (id<MASConstraint> (^)(CGFloat divider))dividedBy;
- (MASConstraint * (^)(CGFloat divider))dividedBy;

/**
* Sets the NSLayoutConstraint priority to a float or MASLayoutPriority
*/
- (id<MASConstraint> (^)(MASLayoutPriority priority))priority;
- (MASConstraint * (^)(MASLayoutPriority priority))priority;

/**
* Sets the NSLayoutConstraint priority to MASLayoutPriorityLow
*/
- (id<MASConstraint> (^)())priorityLow;
- (MASConstraint * (^)())priorityLow;

/**
* Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium
*/
- (id<MASConstraint> (^)())priorityMedium;
- (MASConstraint * (^)())priorityMedium;

/**
* Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh
*/
- (id<MASConstraint> (^)())priorityHigh;
- (MASConstraint * (^)())priorityHigh;

/**
* Sets the constraint relation to NSLayoutRelationEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* see readme for more details.
*/
- (id<MASConstraint> (^)(id attr))equalTo;
- (MASConstraint * (^)(id attr))equalTo;

/**
* Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* see readme for more details.
*/
- (id<MASConstraint> (^)(id attr))greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))greaterThanOrEqualTo;

/**
* Sets the constraint relation to NSLayoutRelationLessThanOrEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* see readme for more details.
*/
- (id<MASConstraint> (^)(id attr))lessThanOrEqualTo;
- (MASConstraint * (^)(id attr))lessThanOrEqualTo;

/**
* optional semantic property which has no effect but improves the readability of constraint
*/
- (id<MASConstraint>)with;
- (MASConstraint *)with;

/**
* Sets the constraint debug name
*/
- (id<MASConstraint> (^)(id key))key;
- (MASConstraint * (^)(id key))key;


// NSLayoutConstraint constant Setters
Expand Down Expand Up @@ -146,7 +146,7 @@
/**
* Whether or not to go through the animator proxy when modifying the constraint
*/
@property (nonatomic, copy, readonly) id<MASConstraint> animator;
@property (nonatomic, copy, readonly) MASConstraint *animator;
#endif

/**
Expand Down Expand Up @@ -177,6 +177,6 @@
* Notifies the delegate when the constraint needs to be replaced with another constraint. For example
* A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks
*/
- (void)constraint:(id<MASConstraint>)constraint shouldBeReplacedWithConstraint:(id<MASConstraint>)replacementConstraint;
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint;

@end
Loading

0 comments on commit 455f887

Please sign in to comment.