MASExampleAnimatedView.m
3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// MASExampleAnimatedView.m
// Masonry iOS Examples
//
// 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;
@property (nonatomic, assign) BOOL animating;
@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_makeConstraints:^(MASConstraintMaker *make) {
[self.animatableConstraints addObjectsFromArray:@[
make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
make.bottom.equalTo(view3.mas_top).offset(-padding),
]];
make.size.equalTo(view2);
make.height.equalTo(view3.mas_height);
}];
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
[self.animatableConstraints addObjectsFromArray:@[
make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
make.left.equalTo(view1.mas_right).offset(padding),
make.bottom.equalTo(view3.mas_top).offset(-padding),
]];
make.size.equalTo(view1);
make.height.equalTo(view3.mas_height);
}];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
[self.animatableConstraints addObjectsFromArray:@[
make.edges.equalTo(superview).insets(paddingInsets).priorityLow(),
]];
make.height.equalTo(view1.mas_height);
make.height.equalTo(view2.mas_height);
}];
return self;
}
- (void)didMoveToSuperview {
[self layoutIfNeeded];
if (self.superview) {
self.animating = YES;
[self animateWithInvertedInsets:NO];
}
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
if (!newSuperview) {
self.animating = NO;
}
}
- (void)animateWithInvertedInsets:(BOOL)invertedInsets {
if (!self.animating) return;
int padding = invertedInsets ? 100 : self.padding;
UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding);
for (id<MASConstraint> constraint in self.animatableConstraints) {
constraint.insets(paddingInsets);
}
[UIView animateWithDuration:1 animations:^{
[self layoutIfNeeded];
} completion:^(BOOL finished) {
//repeat!
[self animateWithInvertedInsets:!invertedInsets];
}];
}
@end