Authored by Jonas Budelmann

more priority helpers, new animated example

... ... @@ -463,6 +463,7 @@
DSTROOT = /tmp/Masonry.dst;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Masonry/Masonry-Prefix.pch";
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
... ... @@ -476,6 +477,7 @@
DSTROOT = /tmp/Masonry.dst;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Masonry/Masonry-Prefix.pch";
GCC_TREAT_WARNINGS_AS_ERRORS = YES;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
... ...
... ... @@ -122,8 +122,8 @@
#pragma mark - layout priority
- (id<MASConstraint> (^)(UILayoutPriority))priority {
return ^id(UILayoutPriority priority) {
- (id<MASConstraint> (^)(MASLayoutPriority))priority {
return ^id(MASLayoutPriority priority) {
for (id<MASConstraint> constraint in self.currentChildConstraints) {
constraint.priority(priority);
}
... ... @@ -131,6 +131,27 @@
};
}
- (id<MASConstraint> (^)())priorityLow {
return ^id{
self.priority(MASLayoutPriorityDefaultLow);
return self;
};
}
- (id<MASConstraint> (^)())priorityMedium {
return ^id{
self.priority(MASLayoutPriorityDefaultMedium);
return self;
};
}
- (id<MASConstraint> (^)())priorityHigh {
return ^id{
self.priority(MASLayoutPriorityDefaultHigh);
return self;
};
}
#pragma mark - layout relation
- (id<MASConstraint> (^)(id))equalityWithBlock:(id<MASConstraint> (^)(id<MASConstraint> constraint, id attr))block {
... ...
... ... @@ -8,6 +8,15 @@
#import <Foundation/Foundation.h>
enum {
MASLayoutPriorityRequired = UILayoutPriorityRequired,
MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh,
MASLayoutPriorityDefaultMedium = 500,
MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow,
MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel,
};
typedef float MASLayoutPriority;
@protocol MASConstraint <NSObject>
@property (nonatomic, copy, readonly) id<MASConstraint> (^insets)(UIEdgeInsets insets);
... ... @@ -16,6 +25,9 @@
@property (nonatomic, copy, readonly) id<MASConstraint> (^offset)(CGFloat offset);
@property (nonatomic, copy, readonly) id<MASConstraint> (^percent)(CGFloat percent);
@property (nonatomic, copy, readonly) id<MASConstraint> (^priority)(UILayoutPriority priority);
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityLow)();
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityMedium)();
@property (nonatomic, copy, readonly) id<MASConstraint> (^priorityHigh)();
@property (nonatomic, copy, readonly) id<MASConstraint> (^equal)(id attr);
@property (nonatomic, copy, readonly) id<MASConstraint> (^greaterThanOrEqual)(id attr);
@property (nonatomic, copy, readonly) id<MASConstraint> (^lessThanOrEqual)(id attr);
... ...
... ... @@ -13,7 +13,7 @@
@property (nonatomic, strong, readwrite) MASViewAttribute *secondViewAttribute;
@property (nonatomic, strong) NSLayoutConstraint *layoutConstraint;
@property (nonatomic, assign) NSLayoutRelation layoutRelation;
@property (nonatomic, assign) UILayoutPriority layoutPriority;
@property (nonatomic, assign) MASLayoutPriority layoutPriority;
@property (nonatomic, assign) CGFloat layoutMultiplier;
@property (nonatomic, assign) CGFloat layoutConstant;
@property (nonatomic, assign) BOOL hasLayoutRelation;
... ... @@ -27,7 +27,7 @@
if (!self) return nil;
_firstViewAttribute = firstViewAttribute;
self.layoutPriority = UILayoutPriorityRequired;
self.layoutPriority = MASLayoutPriorityRequired;
self.layoutMultiplier = 1;
return self;
... ... @@ -141,8 +141,8 @@
#pragma mark - layout priority
- (id<MASConstraint> (^)(UILayoutPriority))priority {
return ^id(UILayoutPriority priority) {
- (id<MASConstraint> (^)(MASLayoutPriority))priority {
return ^id(MASLayoutPriority priority) {
NSAssert(!self.hasBeenCommitted,
@"Cannot modify constraint priority after it has been committed");
... ... @@ -151,6 +151,27 @@
};
}
- (id<MASConstraint> (^)())priorityLow {
return ^id{
self.priority(MASLayoutPriorityDefaultLow);
return self;
};
}
- (id<MASConstraint> (^)())priorityMedium {
return ^id{
self.priority(MASLayoutPriorityDefaultMedium);
return self;
};
}
- (id<MASConstraint> (^)())priorityHigh {
return ^id{
self.priority(MASLayoutPriorityDefaultHigh);
return self;
};
}
#pragma mark - layout relation
- (void)setSecondViewAttribute:(id)secondViewAttribute {
... ...
... ... @@ -19,6 +19,7 @@
DD52F254179CADC0005CD195 /* MASExampleSidesView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD52F24E179CADC0005CD195 /* MASExampleSidesView.m */; };
DD52F255179CADC0005CD195 /* MASExampleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD52F250179CADC0005CD195 /* MASExampleViewController.m */; };
DD52F26E179CBA05005CD195 /* libMasonry.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DD52F26D179CBA05005CD195 /* libMasonry.a */; };
DDF3875C179D648D00178773 /* MASExampleAnimatedView.m in Sources */ = {isa = PBXBuildFile; fileRef = DDF3875B179D648D00178773 /* MASExampleAnimatedView.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
... ... @@ -43,6 +44,8 @@
DD52F24F179CADC0005CD195 /* MASExampleViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleViewController.h; sourceTree = "<group>"; };
DD52F250179CADC0005CD195 /* MASExampleViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleViewController.m; sourceTree = "<group>"; };
DD52F26D179CBA05005CD195 /* libMasonry.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libMasonry.a; sourceTree = BUILT_PRODUCTS_DIR; };
DDF3875A179D648D00178773 /* MASExampleAnimatedView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleAnimatedView.h; sourceTree = "<group>"; };
DDF3875B179D648D00178773 /* MASExampleAnimatedView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleAnimatedView.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
... ... @@ -120,6 +123,8 @@
DD52F24A179CADC0005CD195 /* MASExampleConstantsView.m */,
DD52F24D179CADC0005CD195 /* MASExampleSidesView.h */,
DD52F24E179CADC0005CD195 /* MASExampleSidesView.m */,
DDF3875A179D648D00178773 /* MASExampleAnimatedView.h */,
DDF3875B179D648D00178773 /* MASExampleAnimatedView.m */,
);
name = Views;
sourceTree = "<group>";
... ... @@ -205,6 +210,7 @@
DD52F253179CADC0005CD195 /* MASExampleListViewController.m in Sources */,
DD52F254179CADC0005CD195 /* MASExampleSidesView.m in Sources */,
DD52F255179CADC0005CD195 /* MASExampleViewController.m in Sources */,
DDF3875C179D648D00178773 /* MASExampleAnimatedView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
... ...
//
// MASExampleAnimatedView.h
// MasonryExamples
//
// Created by Jonas Budelmann on 22/07/13.
// Copyright (c) 2013 Jonas Budelmann. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MASExampleAnimatedView : UIView
@end
... ...
//
// MASExampleAnimatedView.m
// MasonryExamples
//
// Created by Jonas Budelmann on 22/07/13.
// Copyright (c) 2013 Jonas Budelmann. All rights reserved.
//
#import "MASExampleAnimatedView.h"
@interface MASExampleAnimatedView ()
@property (nonatomic, strong) NSMutableArray *animatableConstraints;
@property (nonatomic, assign) int padding;
@end
@implementation MASExampleAnimatedView
- (id)init {
self = [super init];
if (!self) return nil;
UIView *view1 = UIView.new;
view1.backgroundColor = UIColor.greenColor;
view1.layer.borderColor = UIColor.blackColor.CGColor;
view1.layer.borderWidth = 2;
[self addSubview:view1];
UIView *view2 = UIView.new;
view2.backgroundColor = UIColor.redColor;
view2.layer.borderColor = UIColor.blackColor.CGColor;
view2.layer.borderWidth = 2;
[self addSubview:view2];
UIView *view3 = UIView.new;
view3.backgroundColor = UIColor.blueColor;
view3.layer.borderColor = UIColor.blackColor.CGColor;
view3.layer.borderWidth = 2;
[self addSubview:view3];
UIView *superview = self;
int padding = self.padding = 10;
UIEdgeInsets paddingInsets = UIEdgeInsetsMake(self.padding, self.padding, self.padding, self.padding);
self.animatableConstraints = NSMutableArray.new;
[view1 mas_buildConstraints:^(MASConstraintBuilder *constraints) {
[self.animatableConstraints addObjectsFromArray:@[
constraints.sides.equal(superview).insets(paddingInsets).priorityLow(),
constraints.bottom.equal(view3.mas_top).offset(-padding),
constraints.right.equal(view2.mas_left).offset(-padding),
]];
constraints.size.equal(view2);
constraints.height.equal(view3.mas_height);
}];
[view2 mas_buildConstraints:^(MASConstraintBuilder *constraints) {
[self.animatableConstraints addObjectsFromArray:@[
constraints.sides.equal(superview).insets(paddingInsets).priorityLow(),
constraints.left.equal(view1.mas_right).offset(padding),
constraints.bottom.equal(view3.mas_top).offset(-padding),
]];
constraints.size.equal(view1);
constraints.height.equal(view3.mas_height);
}];
[view3 mas_buildConstraints:^(MASConstraintBuilder *constraints) {
[self.animatableConstraints addObjectsFromArray:@[
constraints.sides.equal(superview).insets(paddingInsets).priorityLow(),
constraints.top.equal(view1.mas_bottom).offset(padding),
]];
//TODO or pass an array
//constraints.height.equal(superview.subviews);
constraints.height.equal(view1.mas_height);
constraints.height.equal(view2.mas_height);
}];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapWithGestureRecognizer:)];
[self addGestureRecognizer:tapGestureRecognizer];
return self;
}
- (void)didTapWithGestureRecognizer:(UITapGestureRecognizer *)tapGestureRecognizer {
self.padding += 20;
UIEdgeInsets paddingInsets = UIEdgeInsetsMake(self.padding, self.padding, self.padding, self.padding);
for (id<MASConstraint> constraint in self.animatableConstraints) {
constraint.insets(paddingInsets);
}
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];
}
@end
... ...
... ... @@ -11,6 +11,7 @@
#import "MASExampleBasicView.h"
#import "MASExampleConstantsView.h"
#import "MASExampleSidesView.h"
#import "MASExampleAnimatedView.h"
static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
... ... @@ -32,6 +33,7 @@ static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
[[MASExampleViewController alloc] initWithTitle:@"Basic" viewClass:MASExampleBasicView.class],
[[MASExampleViewController alloc] initWithTitle:@"Using Constants" viewClass:MASExampleConstantsView.class],
[[MASExampleViewController alloc] initWithTitle:@"Composite sides" viewClass:MASExampleSidesView.class],
[[MASExampleViewController alloc] initWithTitle:@"Basic Animated" viewClass:MASExampleAnimatedView.class],
];
return self;
... ...
... ... @@ -13,7 +13,7 @@
@property (nonatomic, strong) NSLayoutConstraint *layoutConstraint;
@property (nonatomic, assign) NSLayoutRelation layoutRelation;
@property (nonatomic, assign) UILayoutPriority layoutPriority;
@property (nonatomic, assign) MASLayoutPriority layoutPriority;
@property (nonatomic, assign) CGFloat layoutMultiplier;
@property (nonatomic, assign) CGFloat layoutConstant;
@property (nonatomic, assign) BOOL hasLayoutRelation;
... ...