-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathZDFlexLayoutDiv.m
More file actions
188 lines (156 loc) · 4.77 KB
/
ZDFlexLayoutDiv.m
File metadata and controls
188 lines (156 loc) · 4.77 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// ZDFlexLayoutDiv.m
// Demo
//
// Created by Zero.D.Saber on 2019/10/11.
// Copyright © 2019 Zero.D.Saber. All rights reserved.
//
#import "ZDFlexLayoutDiv.h"
#import <objc/runtime.h>
#import "ZDFlexLayout+Private.h"
@implementation ZDFlexLayoutDiv
@synthesize flexLayout = _flexLayout, layoutFrame = _layoutFrame, parent = _parent, children = _children, owningView = _owningView, isRoot = _isRoot, isNeedLayoutChildren = _isNeedLayoutChildren;
#pragma mark - ZDFlexLayoutNodeProtocol
- (BOOL)isFlexLayoutEnabled {
return _flexLayout != nil;
}
- (void)configureFlexLayoutWithBlock:(void (NS_NOESCAPE ^)(ZDFlexLayout * _Nonnull))block {
if (block) {
block(self.flexLayout);
}
}
- (void)addChild:(ZDFlexLayoutView)child {
if (![child conformsToProtocol:@protocol(ZDFlexLayoutViewProtocol)]) {
NSCAssert1(NO, @"don't support the type:%@", child);
return;
}
[self.children removeObject:child];
[self.children addObject:child];
child.parent = self;
[self addChildSubviews:child];
}
- (void)removeChild:(ZDFlexLayoutView)child {
if (![child conformsToProtocol:@protocol(ZDFlexLayoutViewProtocol)]) {
NSCAssert1(NO, @"don't support the type:%@", child);
return;
}
if (![self.children containsObject:child]) {
return;
}
[self.children removeObject:child];
child.parent = nil;
}
- (void)addChildren:(NSArray<ZDFlexLayoutView> *)children {
for (ZDFlexLayoutView view in children) {
[self addChild:view];
}
}
- (void)removeChildren:(NSArray<ZDFlexLayoutView> *)children {
for (ZDFlexLayoutView view in children) {
[self removeChild:view];
}
}
- (void)insertChild:(ZDFlexLayoutView)child atIndex:(NSInteger)index {
if (![child conformsToProtocol:@protocol(ZDFlexLayoutViewProtocol)]) {
NSCAssert1(NO, @"don't support the type:%@", child);
return;
}
[self.children removeObject:child];
NSInteger mapedIndex = index; // realIndex
if (index > self.children.count || index < 0) {
mapedIndex = self.children.count;
}
[self.children insertObject:child atIndex:mapedIndex];
child.parent = self;
child.owningView = _owningView;
}
- (void)removeFromParent {
if (self.parent) {
[self.parent removeChild:self];
}
}
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeZero;
}
- (void)notifyRootNeedsLayout {
if (self.isRoot && self.isNeedLayoutChildren) {
return;
}
if (self.isRoot && !self.isNeedLayoutChildren) {
self.isNeedLayoutChildren = YES;
}
else if (self.parent) {
[self.parent notifyRootNeedsLayout];
}
}
//MARK: Property
- (void)setOwningView:(UIView *)owningView {
if (_owningView != owningView) {
_owningView = owningView;
if (!owningView) {
return;
}
for (ZDFlexLayoutView child in self.children) {
[self addChildSubviews:child];
}
}
}
- (ZDFlexLayout *)flexLayout {
if (!_flexLayout) {
_flexLayout = [[ZDFlexLayout alloc] initWithView:self];
_flexLayout.isEnabled = YES;
}
return _flexLayout;
}
- (NSMutableOrderedSet<ZDFlexLayoutView> *)children {
if (!_children) {
_children = [[NSMutableOrderedSet alloc] init];
}
return _children;
}
- (void)setGone:(BOOL)gone {
self.flexLayout.isIncludedInLayout = !gone;
[self setView:self hidden:gone];
objc_setAssociatedObject(self, @selector(gone), @(gone), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self notifyRootNeedsLayout];
}
- (BOOL)gone {
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
#pragma mark - Private Method
- (void)setView:(ZDFlexLayoutView)view hidden:(BOOL)hidden {
if ([view respondsToSelector:@selector(setHidden:)]) {
[(UIView *)view setHidden:hidden];
}
else {
// exist child has been setted to gone condition
// don't deal it
for (ZDFlexLayoutView child in view.children) {
if (!child.isFlexLayoutEnabled) {
continue;
}
if (!child.flexLayout.isIncludedInLayout) {
continue;
}
[self setView:child hidden:hidden];
}
}
}
- (void)addChildSubviews:(ZDFlexLayoutView)child {
if (!child || !_owningView) {
return;
}
child.owningView = _owningView;
// if ([child isKindOfClass:UIView.class]) {
// [_owningView addSubview:(UIView *)child];
// }
// else {
// for (ZDFlexLayoutView childChild in child.children) {
// childChild.owningView = child.owningView;
// if ([childChild isKindOfClass:UIView.class]) {
// [_owningView addSubview:(UIView *)childChild];
// }
// }
// }
}
@end