Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set view hierarchy lazily(布局计算完成后再设置视图层级) #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions Source/Core/Public/UIView+ZDFlexLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,7 @@ - (void)removeChild:(ZDFlexLayoutView)child {
}

[self.children removeObject:child];

if ([child isKindOfClass:UIView.class]) {
[(UIView *)child removeFromSuperview];
}
else {
[child.children enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(ZDFlexLayoutView _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[child removeChild:obj];
}];
}


child.parent = nil;
child.owningView = nil;
Expand Down Expand Up @@ -164,18 +156,6 @@ - (void)insertChild:(ZDFlexLayoutView)child atIndex:(NSInteger)index {
[self.children insertObject:child atIndex:mapedIndex];
child.parent = self;
child.owningView = self;

if ([child isKindOfClass:UIView.class]) {
[self insertSubview:(UIView *)child atIndex:mapedIndex];
}
else {
for (ZDFlexLayoutView childChild in child.children) {
childChild.owningView = self;
if ([childChild isKindOfClass:UIView.class]) {
[self insertSubview:(UIView *)childChild atIndex:mapedIndex++];
}
}
}
}

- (void)removeFromParent {
Expand Down
22 changes: 22 additions & 0 deletions Source/Core/Public/ZDFlexLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,28 @@ static void YGApplyLayoutToViewHierarchy(ZDFlexLayoutView view, BOOL preserveOri
[view needReApplyLayoutAtNextRunloop];
}
}

ZDResetViewHierarchy(view);
}

/// lazy set view tree
static void ZDResetViewHierarchy(ZDFlexLayoutView view)
{
if (!view) {
return;
}

UIView *superView = [view isKindOfClass:UIView.self] ? (UIView *)view : view.owningView;
NSCAssert(superView != nil, @"view can't be nil");

for (ZDFlexLayoutView child in view.children) {
if ([child isKindOfClass:UIView.self]) {
[superView addSubview:(UIView *)child];
}
else {
ZDResetViewHierarchy(child);
}
}
}

static void ZD_Dispatch_sync_on_main_queue(dispatch_block_t block)
Expand Down
43 changes: 11 additions & 32 deletions Source/Core/Public/ZDFlexLayoutDiv.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ - (void)removeChild:(ZDFlexLayoutView)child {

[self.children removeObject:child];

if ([child isKindOfClass:UIView.class]) {
[(UIView *)child removeFromSuperview];
}
else {
[child.children enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(ZDFlexLayoutView _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[child removeChild:obj];
}];
}

child.parent = nil;
}

Expand Down Expand Up @@ -88,18 +79,6 @@ - (void)insertChild:(ZDFlexLayoutView)child atIndex:(NSInteger)index {
[self.children insertObject:child atIndex:mapedIndex];
child.parent = self;
child.owningView = _owningView;

if ([child isKindOfClass:UIView.class]) {
[_owningView insertSubview:(UIView *)child atIndex:mapedIndex];
}
else {
for (ZDFlexLayoutView childChild in child.children) {
childChild.owningView = child.owningView;
if ([childChild isKindOfClass:UIView.class]) {
[_owningView insertSubview:(UIView *)childChild atIndex:mapedIndex++];
}
}
}
}

- (void)removeFromParent {
Expand Down Expand Up @@ -193,17 +172,17 @@ - (void)addChildSubviews:(ZDFlexLayoutView)child {

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];
}
}
}
// 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